Skip to content

Instantly share code, notes, and snippets.

@ejazhussain
Last active July 29, 2020 20:58
Show Gist options
  • Select an option

  • Save ejazhussain/a89166fb51971fd505d5f290488bc076 to your computer and use it in GitHub Desktop.

Select an option

Save ejazhussain/a89166fb51971fd505d5f290488bc076 to your computer and use it in GitHub Desktop.
Get access token using On Behalf of Flow - Single Sign On Approach
public async Task<string> GetOnBehalfAccessTokenAsync(string graphScopes, string jwtToken)
{
if (jwtToken == null)
{
throw new ArgumentNullException(jwtToken, "tokenValidationContext.SecurityToken should be a JWT Token");
}
UserAssertion userAssertion = new UserAssertion(jwtToken, "urn:ietf:params:oauth:grant-type:jwt-bearer");
IEnumerable<string> requestedScopes = graphScopes.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries).ToList();
var confidentialClientApp = ConfidentialClientApplicationBuilder.Create(azureAdOptions.Value.ClientId)
.WithAuthority(azureAdOptions.Value.Authority)
.WithClientSecret(azureAdOptions.Value.ClientSecret)
.Build();
var result = await confidentialClientApp.AcquireTokenOnBehalfOf(
requestedScopes.Except(scopesRequestedByMsalNet),
userAssertion)
.ExecuteAsync();
return result.AccessToken;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment