Get the ID of the current user ASP.NET Membership
Asked Answered
R

6

14

How do you guys manage to get the ID of the current user using the ASP.NET Membership Provider? My application is an MVC 3 using Database First approach, so I created my own database and the Membership Provider and Role Provider are using my tables (Custom Membership and Role Provider).

In one of my page (feedback), user can send feedback about my website and if he's connected, I want to include the UserID in the table when I insert.

But since the ASP.NET Membership is an IIdentity, the only thing I can access is the Name. I don't want to create a link between 2 tables using the name, I want the ID!

So what do you do in your application for this VERY simple task? I mean, in almost every page where a connected user needs to insert a value, we need the ID of the user to create the correct foreign key to the user table, right? By the way, I'm very new to this MVC framework, before that I was using ASP.NET aspx and for the user information, I was using the Session which gave me the current ID, Name and other fields as needed. Since this implementation using the Session object gave me trouble in shared hosting, I want to use the real membership.

Thanks!

EDIT

The ID I want must be an integer (in fact, I want the UserID field that is in the User table).

Rms answered 31/10, 2012 at 13:27 Comment(5)
There can be lots of "current users" just do a sql query based on the username.Euraeurasia
Have you tried: (Guid)(Membership.GetUser().ProviderUserKey)?Disfigure
@TimSchmelter: Yes. But this gives a Guid. My feedback table takes an integer ID.Folkway
Membership database holds a GUID or UNIQUEIDENTIFIER for UserId. You won't be getting an Integer.Melodie
Then you need to map your integer with the GUID. I've asked a related question some time ago: https://mcmap.net/q/827810/-how-to-combine-using-membership-api-with-own-application-related-data/284240Disfigure
M
31

Try this;

MembershipUser mu = Membership.GetUser("username");
string userId = mu.ProviderUserKey.ToString();

For currently logged in user you could use without passing the username;

string userId = Membership.GetUser().ProviderUserKey.ToString();
Melodie answered 31/10, 2012 at 13:33 Comment(6)
yes it is, you can cast it out as Guid if you need it. Guid userId = (Guid)(Membership.GetUser().ProviderUserKey)Melodie
But I need a integer user ID (the one in the user table)Folkway
Membership system has aspnet_Users table in which userId is a Uniqueidentifier/Guid not an int.Melodie
Agreed, the MS Membership/Role providor database does not use an integer-like type for IDs. the only identifying value is a GUID/UniqueIdentifier.Majoriemajority
So I whould modify the database to use guid instead of int to make the foreign key between my table feedback and my table user?Folkway
Main thing is that you have to keep a 1:1 relationship between two user tables. Changes beyond that you have to decide depending on the number of objects and data affected.Melodie
D
4

I wanted to share how to get the UserId for Identity 2.0:

string UserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
Dragonroot answered 22/12, 2014 at 21:45 Comment(0)
S
0

I ran into a similar problem. I (for other reasons, as well) made a base Controller class that all of my other controllers inherited from. You can have the base class hold the user's information for use at any time.

Steel answered 31/10, 2012 at 13:33 Comment(0)
M
0

assuming you have everything configured correctly for your providor in your web.config, and since you can get the username, I recommend:

Dim muser As MembershipUser = Membership.GetUser(userName)

Here are details on the MembershipUser object: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28SYSTEM.WEB.SECURITY.MEMBERSHIPUSER%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22%29;k%28DevLang-VB%29&rd=true

many properties other than name available. The ID feild you want is called 'ProviderUserKey'

Majoriemajority answered 31/10, 2012 at 13:33 Comment(0)
R
0

Could this be what your after?

userid from asp.net Membership Authentication?

Personally I have my own class which encapsulates retrieving these details by the username plus any custom data I require in my application.

Regards

Rhodolite answered 31/10, 2012 at 13:40 Comment(0)
C
0

To get current UserID you can try following

string currentUserId= Convert.ToString(Membership.GetUser().ProviderUserKey);
Columbite answered 9/4, 2014 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.