Unable to get Roles Claims on client
- [x] I read and understood how to enable logging
Issue / Steps to reproduce the problem
- Upgraded from dotnet core 1.1 to 2.0 when the new Identity Server 4 was released
- Followed the Microsoft 2.0 migration page
- Followed the Microsoft Identity migration page
- Checked the latest MVCHybrid sample to see how to update my sever and client code
- Have everything working again as it was before the upgrade except I don't get back the user's Roles.
So I initially believed this was related to the part of the Identity Migration page where they took away the ASPNetUser.Roles EF navigation, however fixing this didn't fix my problem. I then noticed the IdentityServer4 logs were returning this:
IdentityServer4.ResponseHandling.UserInfoResponseGenerator:Information: Profile service returned to the following claim types: sub role role role role preferred_username name email
which perfectly matches with the user who has 4 roles assigned to them. So it seems that Identity Server is seeing the roles and then the client is just not adding them to the User.Claims like it is all the other claims (I can see the email, username, sub, sid)
I'm using a custom profile that returns the roles on the server like so:
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResource(
name: "custom.profile",
displayName: "Custom Profile",
claimTypes: new[] { "name", "email", "role" })
};
}
I even tried hacking in
{
UserClaims = {"role"}
}
Which is something that was explained on an earlier issue with someone needing the roles for a API Resource. The Client configuration on the server has:
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"custom.profile"
},
Meanwhile the Client's authentication configuration looks like this:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "oidc";
}).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, x =>
{
//Stuff I don't need in 2.0 anymore?
// x.Cookie.Domain = "Prime.Auth.Core";
// x.Cookie.HttpOnly = true;
// x.Cookie.SecurePolicy = CookieSecurePolicy.Always;
// //x.AutomaticAuthenticate = true;
// //x.AutomaticChallenge = false;
// x.ExpireTimeSpan = TimeSpan.FromDays(1);
}).AddOpenIdConnect("oidc", x =>
{
x.SignInScheme = "Cookies";
x.Authority = AppSettings.AuthUrl;
x.RequireHttpsMetadata = true;
x.SignedOutRedirectUri = AppSettings.CoreUrl;
x.ClientId = "core";
x.ClientSecret = "########";
x.ResponseType = "code id_token";
x.GetClaimsFromUserInfoEndpoint = true;
x.SaveTokens = true;
x.Scope.Add("custom.profile");
x.TokenValidationParameters = new TokenValidationParameters() { NameClaimType = "name", RoleClaimType = "role" };
});
If I set a breakpoint in the client's server code after authenticating I see the User object filled out with the user's details and the Claims property has all the claims except the roles.
Is there something I'm missing to tell Identity Server to send the roles, or am I missing something in the client to tell it not to throw out the roles?
Been stuck on this for a few days so I hope I'm not missing something super simple from the examples you have up and wasting all of your time. Thanks!
I would do
- breakpoint in profile service to see what is actually returned
- check the raw JWT to see what's in there
Thanks for the help. I'm using the built in Profile Service from services.AddIdentityServer().AddAspNetIdentity<ApplicationUser>(); so I wasn't able to put a breakpoint in there. I ended up doing the following on the clients ConfigureServices:
.AddOpenIdConnect("oidc", x =>
{
//other stuff cut out
x.Events = new OpenIdConnectEvents
{
OnUserInformationReceived = usr =>
{
return Task.FromResult(usr);
}
};
and got back the following:

So it's receiving the roles, it's just not populating the User object with these roles. I thought that's what the x.TokenValidationParameters = new TokenValidationParameters() { NameClaimType = "name", RoleClaimType = "role" }; line was supposed to do.
Let me know if I'm leaving the realm of IdentityServer4 and should create a new issue with Microsoft and the Identity team
OK - interesting.
I think Microsoft expects you to map those claims yourself. Have a look at their ClaimsAction property on the new OIDC handler.
oh - and let us know how you solved it ;)
Further and further down the rabbit hole... :)
Looks like this might be an area Identity is trying to kill since Role based security has been mostly replaced by claims based. Sadly I'm trying to drag an 8 year old system out of the dark ages by using an Identity Server for auth, and then having a new server and an old server that both use the single sign on. This lets me incrementally pull old functionality out of the old server and move it over to the new one. Of course the 8 year old server is all Role based so I can't just scrap this.
You mentioning ClaimsAction lead me to this stackoverflow question
and then I also found this on the Aspnet Security github
Both look like they will solve my problem. Thanks @leastprivilege !
Edit: Ohh I forgot to mention the TL;DR of this whole thing is that the default claims handler in .Net Core 2.0 can't handle array based claims (aka, one role claim coming back with 4 values that you then want to map into 4 separate claims with the same key). This is why all of my other claims came across fine except the roles.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.