Search code examples
c#asp.net-mvc-4simplemembership

Get logged in user's id


How can I get the logged in user's UserId? I'm using the standard system generated AccountModel. I can get the username using:

User.Identity.Name

but I don't see the UserId field. I want to use the UserId as a foreign key for another table.


Solution

  • I think you're looking for ProviderUserKey - Gets the user identifier from the membership data source for the user.

    object id = Membership.GetUser().ProviderUserKey
    

    Membership.GetUser() - Gets the information from the data source and updates the last-activity date/time stamp for the current logged-on membership user.