microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dev

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

46lines · modeblame

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