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

pupeno picture pupeno · May 29, 2009 · Viewed 83.6k times · Source

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?

Answer

pupeno picture pupeno · May 29, 2009

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

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