Microsoft Graph create class to handle authentication with Azure AD and use access token on subsequent requests.
public class MsalAuthenticationProvider : IAuthenticationProvider | |
{ | |
private IConfidentialClientApplication _clientApplication; | |
private string[] _scopes; | |
public MsalAuthenticationProvider(IConfidentialClientApplication 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.AcquireTokenForClient(_scopes).ExecuteAsync(); | |
return authResult.AccessToken; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment