Last active
July 29, 2020 20:58
-
-
Save ejazhussain/a89166fb51971fd505d5f290488bc076 to your computer and use it in GitHub Desktop.
Get access token using On Behalf of Flow - Single Sign On Approach
This file contains hidden or 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 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