microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Api/Auth/ClientCredentials.cs
46lines · modeblame
82a4e3c3Rajan1 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. | |
| 3 | | |
73e7847aAlex Acebo1 years ago | 4 | using Microsoft.Teams.Common.Http; |
| 5 | | |
| 6 | namespace Microsoft.Teams.Api.Auth; | |
| 7 | | |
| 8 | public class ClientCredentials : IHttpCredentials | |
| 9 | { | |
| 10 | public string ClientId { get; set; } | |
| 11 | public string ClientSecret { get; set; } | |
| 12 | public string? TenantId { get; set; } | |
| 13 | | |
| 14 | public ClientCredentials(string clientId, string clientSecret) | |
| 15 | { | |
| 16 | ClientId = clientId; | |
| 17 | ClientSecret = clientSecret; | |
| 18 | } | |
| 19 | | |
| 20 | public ClientCredentials(string clientId, string clientSecret, string? tenantId) | |
| 21 | { | |
| 22 | ClientId = clientId; | |
| 23 | ClientSecret = clientSecret; | |
| 24 | TenantId = tenantId; | |
| 25 | } | |
| 26 | | |
| 27 | public async Task<ITokenResponse> Resolve(IHttpClient client, string[] scopes, CancellationToken cancellationToken = default) | |
| 28 | { | |
| 29 | var tenantId = TenantId ?? "botframework.com"; | |
| 30 | var request = HttpRequest.Post( | |
| 31 | $"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token" | |
| 32 | ); | |
| 33 | | |
| 34 | request.Headers.Add("Content-Type", ["application/x-www-form-urlencoded"]); | |
| 35 | request.Body = new Dictionary<string, string>() | |
| 36 | { | |
| 37 | { "grant_type", "client_credentials" }, | |
| 38 | { "client_id", ClientId }, | |
| 39 | { "client_secret", ClientSecret }, | |
bfbe94f7Alex Acebo1 years ago | 40 | { "scope", string.Join(",", scopes) } |
73e7847aAlex Acebo1 years ago | 41 | }; |
| 42 | | |
| 43 | var res = await client.SendAsync<TokenResponse>(request, cancellationToken); | |
| 44 | return res.Body; | |
| 45 | } | |
| 46 | } |