private async Task<string> GetAccessToken()
{
var clientId = _configuration["AzureAd:ClientId"];
var clientSecret = _configuration["AzureAd:ClientSecret"];
var tenantId = _configuration["AzureAd:TenantId"];
var cca = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.Build();
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
var result = await cca.AcquireTokenForClient(scopes)
.ExecuteAsync();
return result.AccessToken;
}
核心是使用 Graph API ConfidentialClientApplicationBuilder 以及 AcquireTokenForClient
这个Scopes为上面的固定默认值