92

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?

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
jasonpresley
  • 1,003
  • 1
  • 7
  • 8

5 Answers5

120

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;.

Anthony Chu
  • 37,170
  • 10
  • 81
  • 71
  • See also: http://stackoverflow.com/questions/20925822/asp-mvc5-identity-how-to-get-current-applicationuser – Chris Patterson May 03 '14 at 03:02
  • 3
    That seems to be not working with Identity 2. Any suggestion please – Ammar Khan Jun 04 '14 at 11:43
  • 2
    Should still be there. I'm using GetUserId() in my Identity 2 projects right now. – Anthony Chu Jun 04 '14 at 16:01
  • 4
    In Identity 3.0 this looks to have moved to the System.Security.Principal namespace. – Sam Feb 27 '15 at 03:55
  • Is there any method to get current user role? when I should set the role? Note: I am using custom storage provider .. – Jeeva J Mar 06 '15 at 10:21
  • Okay, as of beta4 it looks like it’s now in System.Security.Claims and applies to a ClaimsPrincipal instead of an identity – Sam Jun 16 '15 at 08:32
  • Why is this answer marked as the right one? The question is "where is GetUserId in ASP.NET Core?" https://stackoverflow.com/questions/51765214/get-user-id-in-asp-net-core-2 – thomasgalliker Apr 14 '19 at 11:38
63

In order to get CurrentUserId in Asp.net Identity 2.0, at first import Microsoft.AspNet.Identity:

C#:

using Microsoft.AspNet.Identity;

VB.NET:

Imports Microsoft.AspNet.Identity


And then call User.Identity.GetUserId() everywhere you want:

strCurrentUserId = User.Identity.GetUserId()

This method returns current user id as defined datatype for userid in database (the default is String).

Moshtaf
  • 4,833
  • 2
  • 24
  • 34
  • 2
    could you tell me where can I found the corresponding DLL for Microsoft.AspNet.Identity namespace? – Franziee Apr 08 '15 at 09:36
16

Just in case you are like me and the Id Field of the User Entity is an Int or something else other than a string,

using Microsoft.AspNet.Identity;

int userId = User.Identity.GetUserId<int>();

will do the trick

Paul Plato
  • 1,471
  • 6
  • 28
  • 36
7

I had the same issue. I am currently using Asp.net Core 2.2. I solved this problem with the following piece of code.

using Microsoft.AspNetCore.Identity;
var user = await _userManager.FindByEmailAsync(User.Identity.Name);

I hope this will be useful to someone.

Mohsin
  • 465
  • 6
  • 15
5

I used Claims to get the userId, username and email of the logged in user.

            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // will give the user's userId
        //userName = User.FindFirstValue(ClaimTypes.Name); // will give the user's userName
        //email = User.FindFirstValue(ClaimTypes.Email);
Nicolas
  • 156
  • 2
  • 2