0

I'm using implicit grant type, and when I request "id_token token" as response type my HttpContext.Current.User is null after logging in leading me to believe something has gone wrong inside owin. If I just have "id_token" as response type its fine. Do I need to tell owin somewhere to get the access token?

For reference I'm using .Net Framework as my client and identityserver4.

1 Answer 1

1

To be able to get the token via browser you need to set AllowAccessTokensViaBrowser = true on client's config within IdentityServer:

                new Client
                {
                    ...

                    AllowedGrantTypes = GrantTypes.Implicit,
                    AllowAccessTokensViaBrowser = true,

                    ...
                },

and on MVC client's Startup, to you can add the access_token as a claim to user:

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                ...
                ResponseType = "id_token token",             

                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    SecurityTokenValidated = n =>
                    {
                        n.AuthenticationTicket.Identity.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken));
                        
                        return Task.FromResult(0);
                    }
                }
            });

I have the full working sample here

4
  • Awesome thank you! As a follow up, how would I use this access token to talk to a local API in identity server?
    – Josh
    Jun 28, 2020 at 21:01
  • just get the access_token and set it in request header when calling api. Here is an example github.com/nahidf-adventures/IdentityServer4-adventures/blob/…. only difference is that in your case you get the access_token from user's claims. Local API in IDS4 is like any other API
    – nahidf
    Jun 28, 2020 at 21:58
  • Nevermind - figured it out. If anyone else is wondering this worked for me: HttpClient client = new HttpClient(); client.BaseAddress = new Uri("localhost:5000/api/Users/TestMethod"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenHelper.GetAccessToken()); Then just continue with a usual http request. FYI the tokenHelper just gets me the value of the access_token claim. Thanks again nahidf for your help!
    – Josh
    Jun 29, 2020 at 4:01
  • 1
    @Josh np, happy could help, feel free to read my blog about it nahidfa.com/posts/identityserver4-and-asp-.net-mvc/…
    – nahidf
    Jun 29, 2020 at 17:42

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.