How do you get the UserID of a User object in ASP.Net MVC?

I have several tables with UserID that is related to aspnet_Users.UserID. When the user sends some data for these tables, since the controller method has [Authorize], I get a User object. I can get the username with the name User.Identity.Name, but how to get the user ID to establish the relationship (owner)?

+72
membership
May 29 '09 at 6:45 a.m.
source share
11 answers

It seems you cannot get it from the User object, but you can get it like this:

Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey; 
+85
May 29 '09 at 19:58
source share

Here is the solution:

Include:

 using Microsoft.AspNet.Identity; 

Then use extension methods:

 User.Identity.GetUserId(); 
+30
Jan 30 '14 at 12:55
source share

First, this answer is not strictly an MVC answer, but an ASP.NET answer. The fact that your MVC site is not relevant to solving the problem, in this case.




Hm. I'm not very sure how you handle your users on your system, but it looks like you are using the (very malicious) asp.net membership provider , which comes out of the box with .net. It hints at what you said

  • aspnet_Users.UserID
  • UserID is a unique identifier (read: GUID).

When using the default authentication system, which uses FormsIdentity by default, it has only one property called Name (as you correctly noted). This means that it has only one value, where you can place some unique user information. In your case, you put Name / UserName / DisplayName in the Name property. I assume this name is the display name, and it is unique. No matter what value you put in this place, it MUST BE UNIQUE.

From this you can grab a custom pointer.

Check this.

 using System.Web.Security; .... // NOTE: This is a static method .. which makes things easier to use. MembershipUser user = Membership.GetUser(User.Identity.Name); if (user == null) { throw new InvalidOperationException("User [" + User.Identity.Name + " ] not found."); } // Do whatever u want with the unique identifier. Guid guid = (Guid)user.ProviderUserKey; 

So, every time you want to get information about a user, you need to take it from the database using the static method above.

Read all about the membership class and the MembershipUser class on MSDN.

Bonus Response / Offer

That way I would do CACHE, so you don't need to keep getting into the database.

 ... cont from above.... Guid guid = (Guid)user.ProviderUserKey; Cache.Add(User.Identity.Name, user.UserID); // Key: Username; Value: Guid. 

Otherwise, you can create your own Identity class (which inherits from IIdentity ) and add your own custom properties, such as UserID . Then, whenever you authenticate (as well as for each request), you can set this value. Anyway, this is a solid decision, so let's go with caching right now.

NTN

+27
May 29 '09 at 12:36
source share

User.Identity is IPrincipal - typically of type System.Web.Security.FormsIdentity

He knows nothing about UserIDs - it's just an abstraction of the concept of "identity."

The IIdentity interface has only a username, not even a "Username".

If you are using MVC4 with the standard SimpleMembershipProvider , you can do this:

 WebSecurity.GetUserId(User.Identity.Name) // User is on ControllerBase 

(Where WebSecurity is in the nuget package Microsoft.AspNet.WebPages.WebData in WebMatrix

You can also use

 WebSecurity.CurrentUserName WebSecurity.CurrentUserId 

(if you are using ASPNetMembershipProvider , which is an older, more complex ASPNET membership system, then see answer by @ eduncan911)

+18
Jan 17 '13 at 3:08
source share

If you use ASP.NET membership (which in turn uses the IPrincipal object):

 using System.Web.Security; { MembershipUser user = Membership.GetUser(HttpContext.User.Identity.Name); Guid guid = (Guid)user.ProviderUserKey; } 

User.Identity always returns the status of the current user, registered or not.

Anonymous or not, etc. Thus, the login check:

 if (User.Identity.IsAuthenticated) { ... } 

So, all together:

 using System.Web.Security; { if (User.Identity.IsAuthenticated) { MembershipUser user = Membership.GetUser(HttpContext.User.Identity.Name); Guid guid = (Guid)user.ProviderUserKey; } } 
+11
May 29 '09 at 20:08
source share

Best option to get user id

Add below links

 using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Owin.Security;* public myFunc() { ..... // Code which will give you user ID is var tmp = User.Identity.GetUserId(); } 
+6
Jan 09 '14 at 2:39 on
source share

If you use your own IPrincipal object for authorization, you just need to send it to access the identifier.

For example:

 public class MyCustomUser : IPrincipal { public int UserId {get;set;} //...Other IPrincipal stuff } 

Here's a great tutorial on creating your own forms-based authentication.

http://www.codeproject.com/KB/web-security/AspNetCustomAuth.aspx

This should lead you to the right way to create an authentication cookie for your user and access your user data.

+3
May 29 '09 at 14:39
source share
 using System.Web.Security; MembershipUser user = Membership.GetUser(User.Identity.Name); int id = Convert.ToInt32(user.ProviderUserKey); 
+2
Feb 27 '14 at 18:08
source share

Property ProviderUserKey.

 System.Web.Security.MembershipUser u; u.ProviderUserKey 
+1
May 29 '09 at 7:17
source share

Plain....

 int userID = WebSecurity.CurrentUserId; 
+1
Aug 05 '13 at 9:54 on
source share

Usually you can just use WebSecurity.currentUserId , but if you are in AccountController right after creating an account and want to use a user ID to associate a user with some data in other tables, then WebSecurity.currentUserId (and all the solutions above), unfortunately, in this case returns -1, so it does not work.

Fortunately, in this case you have a db context for the UserProfiles table, so you can get the user ID as follows:

 UserProfile profile = db.UserProfiles.Where( u => u.UserName.Equals(model.UserName) ).SingleOrDefault(); 

I recently met this thing, and this answer would save me a whole bunch of time, so just putting it there.

0
Dec 15 '15 at 10:55
source share



All Articles