Get current user id in ASP.NET Identity 2.0

jasonpresley picture jasonpresley · Mar 25, 2014 · Viewed 114k times · Source

I just switched over to using the new 2.0 version of the Identity Framework. In 1.0 I could get a user object by using manager.FindByIdAsync(User.Identity.GetUserId()). The GetUserId() method does not seem to exists in 2.0.

Now all I can figure out is to use manager.FindByEmailAsync(User.Identity.Name) which references the username field in the users table. In my application this is set to the same as the email field.

I can see this causing issues down the road when someone needs to update their email. Is there a way to get the current logged in user object based off an unchanging value (such as the id field) in the Identity 2.0 Framework?

Answer

Anthony Chu picture Anthony Chu · Mar 25, 2014

GetUserId() is an extension method on IIdentity and it is in Microsoft.AspNet.Identity.IdentityExtensions. Make sure you have added the namespace with using Microsoft.AspNet.Identity;.