After solving the problem I had yesterday, my OIDC middleware in ASP.NET Core was finally working. I was able to login and I could find all my claims inside my ClaimsIdentity.
However this was not the end of all my problems as I noticed that the User.Identity.Name value was empty. Strange! Because when I took a look at my claims, a name claim was certainly there…
What is going on?
The thing is that Microsoft provided a NameClaimType and also a RoleClaimType property on ClaimsIdentity. These properties define which claim should be used to represent the name(and role) claim on your User.Identity. As a default value they decided on using the following claimtypes (which were part of WIF):
- http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name
- http://schemas.microsoft.com/ws/2008/06/identity/claims/role
These claimtypes are not part of the OIDC claim types and this explains why no mapping is happening…
To fix this you can update your OIDC middleware by adding a TokenValidationParameters section:
var oidcOptions = new OpenIdConnectOptions | |
{ | |
AuthenticationScheme = "oidc", | |
SignInScheme = "cookies", | |
Authority = "http://localhost:5000", | |
ClientId = "mvcsample", | |
ResponseType = "id_token", | |
SaveTokens = true, | |
TokenValidationParameters = new TokenValidationParameters | |
{ | |
NameClaimType = "name", | |
RoleClaimType = "role", | |
} | |
}; |