HttpContextAccessor returns the Email of Logged in user in Identity Server 4

Prathamesh Shende 381 Reputation points
2023-12-02T16:20:40.1266667+00:00

I am learning the identity server 4, about the SSO authentication.
I want to get httpContextAccessor with email which is logged in using Identity Server 4. I set scope - Email and Name.
But it will not working.

Here is my code of client webApi project.

builder.Services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
    if (builder.Environment.IsDevelopment())
    {
        options.Authority = "https://localhost:44385";
    }
   
    options.Audience = "api1";
    options.RequireHttpsMetadata = false;
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateAudience = false,
    };
});

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("ApiScope", policy =>
    {
        policy.RequireAuthenticatedUser();
        policy.RequireClaim("scope", "api1");
    });
});
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,684 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 68,306 Reputation points
    2023-12-02T23:32:52.2666667+00:00

    where is the code where you try to access the email claim? also we would need to see how you configured identity server to return the desired claims.

    note: identity server 4 is a 3rd party product which no longer supported. you should be using one of their supported products.

    https://identityserver4.readthedocs.io/en/latest/index.html

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.