How to get the id of the current registered user?

I am working on a sample project created from an ASP.NET web application template (web forms, not MVC). After the user logs in, I want to get the user ID and save it in a session variable so that I can transfer it between pages. How to get user id from ASP.NET membership provider? By user ID, I mean the GUID in the membership database. Is there any way to get user id?

I use the default database schema and deploy it to my server.

Thanks!

+4
source share
3 answers

Try the following:

MembershipUser membershipUser = Membership.GetUser(); string UserID = membershipUser.ProviderUserKey.ToString(); 
+8
source

You do not need to skip the user ID. The authenticated user is currently stored in HttpContext.Current.User .

+5
source

you can define a static method in a class and use it:

 public static Guid getCurrentUserGUID(){ if (HttpContext.Current.User.Identity.IsAuthenticated) { MembershipUser myObject; myObject = Membership.GetUser(HttpContext.Current.User.Identity.Name); return (Guid)myObject.ProviderUserKey; } return Guid.Empty; } 
+1
source

All Articles