question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BootstrapContext is NULL after TokenValidationParameters is shifted to Microsoft.IdentityModel.Tokens

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
GauravDhavalecommented, Feb 13, 2018

I used the BootstrapContext value directly in the userAssertion. It helps to get the AccessToken.

  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;
                UserAssertion userAssertion = new UserAssertion(bc.ToString(), "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;
                Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
                ClientCredential cred = new ClientCredential(clientId, appKey);
                result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", cred, userAssertion);
                UserAccessToken = result.AccessToken;
            });

        }
0reactions
brentschmaltzcommented, Mar 23, 2018

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net mvc - BootstrapContext is null on ClaimsIdentity
Now I need the bootstrap token to secure the calls to my service layer. I have set the saveBootstrapContext to true on the...
Read more >
ClaimsIdentity.BootstrapContext Property
If the BootstrapContext property is not null , applications can access the original token and the claims it produced through the properties and...
Read more >
Access Token validating fails with JWTSecurityTokenHandler
I created the token the following way: 1. ... with JWTSecurityTokenHandler, but die signature is invalid: Microsoft.IdentityModel.Tokens.
Read more >
BootstrapContext.Token Property (System.IdentityModel. ...
The string that was used to initialize the context or null . Remarks. This property is only set if the BootstrapContext.BootstrapContext(String) constructor was ......
Read more >
Workflow.Client 5.2 API dll fails to connect when running in ...
It's odd though because BootStrap context is null on my anonymous windows context that I'm using for my work-around, and the Connection.Open ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found