BootstrapContext is NULL after TokenValidationParameters is shifted to Microsoft.IdentityModel.Tokens
See original GitHub issueI was facing the TokenValidationParameters related error as highlighted in this. So, I updated the related packages to the preview versions as suggested in this issue and also updated TVP namespace to Microsoft.IdentityModel.Tokens
from System.IdentityModel.Tokens
. However, I’m facing issues while fetching the access token using the ClaimsPrincipal
and BootstrapContext
as it’s in the namespace System.IdentityModel.Tokens
.
My code in Startup.Auth.cs file is as follows:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
RedirectUri = postLogoutRedirectUri,
TokenValidationParameters = new TokenValidationParameters
{
SaveSigninToken = true
},
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthorizationCodeReceived = async context =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationContext authContext = new AuthenticationContext(authority);
AuthenticationResult ar = await authContext.AcquireTokenByAuthorizationCodeAsync(code, new Uri(RedirectUri), credential, "https://graph.windows.net");
},
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/Error?message=" + context.Exception.Message);
return Task.FromResult(0);
}
}
});
}
In another file in Home controller, I want to get the user information along with Access Token using below code:
private Task GetTokenViaBootStrap()
{
return Task.Run(async () =>
{
var bc = ClaimsPrincipal.Current.Identities.First().BootstrapContext as BootstrapContext;
string userName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn) != null ? ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn).Value : ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value;
string userAccessToken = bc.Token;
UserAssertion userAssertion = new UserAssertion(bc.Token, "urn:ietf:params:oauth:grant-type:jwt-bearer", userName);
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationContext authContext = new AuthenticationContext(authority);
ClientCredential cred = new ClientCredential(clientId, appKey);
result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", cred, userAssertion);
UserAccessToken = result.AccessToken;
});
}
The ClaimsPrincipal.Current.Identities.First().BootstrapContext
part contains the value but when I tried to get its parameters using as BootstrapContext
it gives NULL.
In the above code, I’m getting a NULL value for bc. Any help to resolve this issue?
PS: I have also posted this issue on TokenValidationParameters error issue thread but since its closed, I created a new issue.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
I used the BootstrapContext value directly in the
userAssertion
. It helps to get theAccessToken
.@GauravDhavale I had missed this. Is this still an issue? If so can you open an issue here: https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet