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

I have some tables that have a uniqueidentifier UserID that relates to aspnet_Users.UserID. When the user submits some data for those tables, since the controller method has an [Authorize] I get a User object. I can get the username with User.Identity.Name, but how do I get the UserID to be able to establish (the ownership) relationship?


Solution 1:

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

Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;

Solution 2:

Here is the solution:

Include:

using Microsoft.AspNet.Identity;

Then use extension methods:

User.Identity.GetUserId();