Last active
October 7, 2019 16:35
-
-
Save BrianTJackett/c69c1991eac2b3f5a76996d17223299a to your computer and use it in GitHub Desktop.
Microsoft Graph create class to handle authentication with Azure AD and use access token on subsequent requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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