I use Identity Server 4 and Swagger in my microservice to authorize. So, I have this configuration on IS side:
public static IEnumerable<ApiScope> ApiScopes =>
new List<ApiScope>()
{
new ApiScope("PetAPI", "Pets WebAPI"),
new ApiScope("NotificationsAPI", "Notifications WebAPI"),
new ApiScope("ScheduleAPI","Schedule WebAPI")
};
public static IEnumerable<IdentityResource> IdentityResources =>
new List<IdentityResource>()
{
new IdentityResources.OpenId(),
new IdentityResources.Email(),
new IdentityResources.Profile()
};
public static IEnumerable<ApiResource> ApiResources =>
new List<ApiResource>()
{
new ApiResource("PetAPI"),
new ApiResource("NotificationsAPI"),
new ApiResource("ScheduleAPI")
};
public static IEnumerable<Client> Clients =>
new List<Client>()
{
new Client()
{
ClientId = "pmcs-client-id",
ClientSecrets = { new Secret("client_secret".ToSha256()) },
ClientName = "M2M Client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
AllowedScopes = {
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"PetAPI",
"NotificationsAPI",
"ScheduleAPI"
}
},
new Client()
{
ClientId = "swagger-client-id",
ClientSecrets = { new Secret("client_secret".ToSha256()) },
ClientName = "Swagger Client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
AllowedScopes = {
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.Profile,
"PetAPI",
"NotificationsAPI",
"ScheduleAPI"
}
}
};
And configuration on microservice side:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
options.Authority = AuthConfiguration.Authority;
options.RequireHttpsMetadata = AuthConfiguration.RequireHttpsMetadata;
options.Audience = AuthConfiguration.Audience;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = AuthConfiguration.ValidateAudience,
};
})
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, config =>
{
config.Authority = AuthConfiguration.Authority;
config.ClientId = AuthConfiguration.SwaggerClientId;
config.ClientSecret = AuthConfiguration.ClientSecret;
config.SaveTokens = true;
config.ResponseType = "id_token";
config.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = AuthConfiguration.ValidateAudience
};
config.Scope.Add(AuthConfiguration.ScheduleScope);
config.Scope.Add("email");
config.Scope.Add("openid");
config.Scope.Add("profile");
config.GetClaimsFromUserInfoEndpoint = true;
config.ClaimActions.MapAll();
});
The method I use to get claims:
var emailFromClaims = _context.HttpContext?.User?.FindFirst(ClaimTypes.Email)?.Value;
It seems that I have this claim in my Id token, but they aren't mapped into user claims. I really can't understand what's wrong and I'll be exremely grateful if anyone could help me to find a solution.
Link to pull request where I came across this problem: https://github.com/nantonov/PMCS/pull/58