Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user id in ASP.NET Core 2

I'm trying to get the user id in an ASP.NET Core 2.1 MVC project.

However, I was only able to get the email. I'm almost sure there has to be a 1/2 line way to get it (in the ASP.NET MVC membership it was just var loggedInUserId = User.Identity.GetUserId();

I tried so far like this:

 var loggedInUserId = User.Identity.ToString();    // Result = Name (E-mail) 
 //  var loggedInUserId = User.Identity.Name;    // Result (E-mail)

& this is now what I need

like image 288
Assaf Our Avatar asked Aug 09 '18 11:08

Assaf Our


People also ask

How do I find my username for net core?

Simple way that works and I checked. private readonly UserManager<IdentityUser> _userManager; public CompetitionsController(UserManager<IdentityUser> userManager) { _userManager = userManager; } var user = await _userManager. GetUserAsync(HttpContext. User);

How can I get username in asp net?

In ASP.NET, please use User.Identity.Name to get the logon user. User.Identity.Name might be empty if Anonymous Authentication is enabled in IIS. Here is the logical to check if the identity is available. ' Gets the name if authenticated.

How can we get logged in user details in asp net core?

You can create a method to get the current user : private Task<ApplicationUser> GetCurrentUserAsync() => _userManager. GetUserAsync(HttpContext. User);

What is HttpContext user identity Name?

It just holds the username of the user that is currently logged in. After login successful authentication, the username is automatically stored by login authentication system to "HttpContext.Current.User.Identity.Name" property.


2 Answers

The old method of User.Identity.GetUserId() no longer exists, but the id is available as a claim on your principal, i.e. User. There's a number of ways you can get to it:

  1. The first and easiest is just pull out the claim:

    var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
    
  2. If you already have an instance of UserManager<TUser> (or want to inject one), then you can use the GetUserId() method on that:

    var userId = _userManager.GetUserId(User);
    
  3. Finally, if you want the old way back, it's as simple as adding an extension to ClaimsPrincipal and utilize the first method above:

    public static class ClaimsPrincipalExtensions
    {
        public static string GetUserId(this ClaimsPrincipal principal) =>
            principal.FindFirstValue(ClaimTypes.NameIdentifier);
    }
    
like image 89
Chris Pratt Avatar answered Oct 10 '22 03:10

Chris Pratt


You need to inject dependency in the controller like shown below:

[Authorize]
public class AccountController: Controller
{
private readonly UserManager<IdentityUser> _userManager;

    public AccountController(UserManager<IdentityUser> userManager) 
    {
        _userManager = userManager;
    }
}

Now, you can use below code anywhere inside that controller to get user details.

var user = await _userManager.FindByEmailAsync(model.EmailID);

Now, You can use user.Id to get userId.

like image 26
Trimantra Software Solution Avatar answered Oct 10 '22 04:10

Trimantra Software Solution