Forum Discussion
ktchoumak
Jul 14, 2021Copper Contributor
Get bearier token for Azure Graph api
I found 3 ways: 1. from MSDN example: result = await app.AcquireTokenForClient(scopes) https://github.com/Azure-Samples/active-directory-dotnetcore-daemon-v2/blob/master/1-Call-MSGraph/daemon-co...
ktchoumak
Jul 14, 2021Copper Contributor
I do not understand why Microsoft using such low informative way in it's snippets?
https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Client-credential-flows
Here is main wrapper:
private static async Task<AuthenticationResult> GetToken(string tenantId, string appId, string clientSecret)
{
// this object will cache tokens in-memory - keep it as a singleton
var singletonApp = ConfidentialClientApplicationBuilder.Create(appId)
.WithClientSecret(clientSecret)
.Build();
// If instead you need to re-create the ConfidentialClientApplication on each request, you MUST customize
// the cache serialization (see below)
// when making the request, specify the tenanted authority
// uses the token cache automatically, which is optimized for multi-tenant access
var authResult = await singletonApp.AcquireTokenForClient(scopes: new[] { "https://graph.microsoft.com/.default" })
.WithAuthority(AzureCloudInstance.AzurePublic, tenantId) // do not use "common" or "organizations"!
.ExecuteAsync();
return authResult;
}