Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Membership: Where is Current User ID Stored?

Using ASP.NET membership, if I want to get information for the current user, I can call MembershipUser.GetUser()

So, somehow the system must know the ID of the current user.

If this is correct, and all I want is the ID of the current user, is there a way to get it without returning all the user information from the database?

I know I can get the username of the current user using User.Identity.Name, but I need the ID.

like image 275
Jonathan Wood Avatar asked Jan 14 '12 04:01

Jonathan Wood


People also ask

How can get current user details in asp net core?

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

How does Membership GetUser work?

The GetUser method retrieves the user information from the data source and creates a MembershipUser object populated with the returned data. If you use one of the GetUser overloads that does not take a username parameter, GetUser returns the information for the current logged-on membership user.


1 Answers

The short answer is no you can't get only userID without retrieve whole user info when you use built-in membership provider, only by this

    MembershipUser user = Membership.GetUser();
    string UserID = user.ProviderUserKey.ToString();

But if you want to have method or property which retrieve only userID, you must re-implement your own membership provider or(it's simply) to implement IPrincipal interface

like image 77
Vitaly Zemlyansky Avatar answered Oct 02 '22 18:10

Vitaly Zemlyansky