microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
docs/update-release-process

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Api/Auth/ClientCredentials.cs

47lines · modecode

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