Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the user id of a username identity 2.0

MVC 5 with asp.net identity 2.0

I understand how to get the currently logged in user id. I have a need to get the user id of a different user in the system. How is this done?

Ex. I need to get the user id of user names "fsmith" ...

like image 607
BTI-Doug Avatar asked Dec 29 '14 13:12

BTI-Doug


People also ask

What is ID user?

A user identification or user ID is an entity used to identify a user on a website, software, system or within a generic IT environment.

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

try this System.Web.HttpContext.Current.User.Identity.Name ? It should work. See the samples in asp.net github.com/aspnet/Identity/blob/…. Just make sure user is logged in.

How to get the user name from the security identifier?

If you happen to have a case where you need to find the user name but all you have is the security identifier, you can "reverse" the command like this (just replace this SID with the one in question): wmic useraccount where sid="S-1-5-21-992878714-4041223874-2616370337-1001" get name

How to get currentuserid in ASPnet identity?

In order to get CurrentUserId in Asp.net Identity 2.0, at first import Microsoft.AspNet.Identity: And then call User.Identity.GetUserId () everywhere you want: This method returns current user id as defined datatype for userid in database (the default is String ).

What is identityuser<TKey>?

The default implementation of IdentityUser<TKey> which uses a string as a primary key. Provides the APIs for managing user in a persistence store. Represents the result of an identity operation.

What is the default key type of identityrole and identityuser?

The default implementation of IdentityRole<TKey> which uses a string as the primary key. The default implementation of IdentityUser<TKey> which uses a string as a primary key. Provides the APIs for managing user in a persistence store.


3 Answers

Inside the AccountController you can call the following method to get user object and then get the User Id.

var user = await UserManager.FindByNameAsync(userName);
var userId = user.Id;

If you are not inside AccountController you can get the UserManager reference using the following code

var UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
like image 127
Shashi Jeevan M. P. Avatar answered Sep 18 '22 17:09

Shashi Jeevan M. P.


To add to Shashi's useful answer...

If you're inside the account controller you can use the existing UserManager:

var user = await UserManager.FindByNameAsync(userName);
var userId = user.Id;

In other controllers (and maybe select other places where HttpContext is available) you create a User Manager with:

var um = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

But what it took me a while to realize is you can get at it just about anywhere with this last bit. For instance I used it in my seed method (in Configuration.cs) so I could seed the User property of another model:

var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(*insert-your-db-context-here*));
like image 44
Methodician Avatar answered Sep 21 '22 17:09

Methodician


One way would to use the FindByNameAsync method of your user manager (or the generic version of it). It takes the name of the user (in your case "fsmith") and returns a task of a user object. This user object contains all the information available about that user.

A code snippet could look similar to the following one (if you're looking for the database ID):

var user = await UserManager.FindByNameAsync("fsmith");
var userId = user.Id;

EDIT

The UserManager instance is normally declared as follows (keep in mind that there are many ways of declaring it, for example when using Dependency Injection):

public AccountController()
  : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}

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

public UserManager<ApplicationUser> UserManager { get; private set; }
like image 24
Horizon_Net Avatar answered Sep 17 '22 17:09

Horizon_Net