Instantly share code, notes, and snippets.

Embed
What would you like to do?
Microsoft Graph create class to handle authentication with Azure AD and use access token on subsequent requests.
public class MsalAuthenticationProvider : IAuthenticationProvider
{
private ConfidentialClientApplication _clientApplication;
private string[] _scopes;
public MsalAuthenticationProvider(ConfidentialClientApplication clientApplication, string[] scopes) {
_clientApplication = clientApplication;
_scopes = scopes;
}
public async Task AuthenticateRequestAsync(HttpRequestMessage request)
{
var token = await GetTokenAsync();
request.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
}
public async Task<string> GetTokenAsync()
{
AuthenticationResult authResult = null;
authResult = await _clientApplication.AcquireTokenForClientAsync(_scopes);
return authResult.AccessToken;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment