IdentityServer / IdentityServer3.AspNetIdentity

ASP.NET Identity support for Thinktecture IdentityServer3
Apache License 2.0
64 stars 51 forks source link

User.Identity.Name is always null #79

Closed brianlocke closed 7 years ago

brianlocke commented 7 years ago

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 sample repository and tested it out with the webforms sample 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

Let me know if this isn't the correct place to put this.

brockallen commented 7 years ago

Let me know if this isn't the correct place to put this.

This is a question for stackoverflow.

omayhemo commented 7 years ago

Can we get a link to the answer at least?

CesarD commented 7 years ago

@omayhemo I think this is the link: http://stackoverflow.com/questions/42441771/user-identity-name-is-always-null-when-using-aspnetidentity-with-identityserver3

But it doesn't have an answer yet :(

brockallen commented 7 years ago

https://leastprivilege.com/2016/08/21/why-does-my-authorize-attribute-not-work/

CesarD commented 7 years ago

Yeah, my solution was to put something like this: ((ClaimsPrincipal)HttpContext.Current.User).FindFirst("sub").Value,

omayhemo commented 7 years ago

Thanks CesarD, I have already implemented a solution, I should've revisited here to share.

If you want to use some of the inherited methods in the Microsoft.AspNet.Identity.UserManager to pull useful claims (such as UserId) and make as few changes as possible to ASP.net Identity Integrations

That'll wire up what ASP.net Identity expect to find using the canned functionality. For example from a MVC controller User.Identity.GetUserId() and the OP original question.

brianlocke commented 7 years ago

I updated my question on the stackoverflow link to say that I continued to manually add the claim. I haven't had the time to dig into why the two sample projects behave differently. Apologies for not keeping up with my own question!