meds
meds

Reputation: 22956

Getting id_token from IdentityServer4?

I have the following flow setup to an IdentityServer4 server:

    services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc2";
        })
        .AddCookie("Cookies")
        .AddOpenIdConnect("oidc2", options =>
        {
            options.SignInScheme = "Cookies";

            options.Authority = "http://localhost:5000";
            options.RequireHttpsMetadata = false;

            options.ClientId = "mvc";
            options.ClientSecret = "secret";
            options.ResponseType = "code id_token";

            options.SaveTokens = true;
            options.GetClaimsFromUserInfoEndpoint = true;

            options.Scope.Add("api1");
            options.Scope.Add("offline_access");
            options.ClaimActions.MapJsonKey("website", "website");

        });

Which works fine, user is authenticated correctly.

I'd like to access the id_token in the response so I can see the users display name.

How do I do that? I've looked around HttpContext.User but didn't find much.

Upvotes: 0

Views: 216

Answers (1)

Richard
Richard

Reputation: 1584

If the user is authenticated correctly then in a MVC controller you will be able to cast the user to a ClaimsPrincipal and access the user's claims:

ClaimsPrincipal user = User as ClaimsPrincipal;
Claim nameClaim = user.FindFirst("name");
string name = nameClaim.Value

Upvotes: 1

Related Questions