Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User.Identity.Name is always null when using AspNetIdentity with IdentityServer3

I've been trying to setup a new IdentityServer3 with AspNetIdentity for a few days now. I'm able to login using my existing Identity DB and that's all good but I can never get the User.Identity.Name to contain data. I've tried multiple attempts at adding custom claims & scopes and adding scopes to clients.

Finally, I loaded up the IdentityServer3 Sample repository and tested it out with the webforms client project since it already used the User.Identity.Name in it's About page.

Using WebForms sample client + AspNetIdentity sample server = User.Identity.Name is always null Using WebForms sample client + SelfHost with Seq sample server = User.Identity.Name with data I've tried other sample host projects that all populate the User.Identity.Name value just fine.

Now, on the client side I've written a workaround to pull the 'preferred_username' claim value and set the 'name' claim with it.

var id = new claimsIdentity(n.AuthenticationTicket.Identity.AuthenticationType);
id.AddClaims(userInfoResponse.GetClaimsIdentity().Claims);

//set the User.Identity.Name value
var name = id.Claims.Where(x => x.Type == "name").Select(x => x.Value).FirstOrDefault() ??
           id.Claims.Where(x => x.Type == "preferred_username").Select(x => x.Value).FirstOrDefault();
id.AddClaim(new Claim("name", name));

My questions are:

  1. Why doesn't the AspNetIdentity package fill this by default?
  2. And what do I need to change on the server side so that I don't need to change the client?
like image 640
TheBrian Avatar asked Feb 24 '17 14:02

TheBrian


1 Answers

public static IEnumerable<ApiResource> GetApis()
{
   return new ApiResource[]
   {
      new ApiResource("MyApi", "My Admin API")
      {
          UserClaims =  { JwtClaimTypes.Name, JwtClaimTypes.Email }
      }
   };
}

In Identityserver4 you can add the UserClaims to your resource. Fixed it for me.

like image 170
Ferre12 Avatar answered Oct 31 '22 12:10

Ferre12