irl_irl
irl_irl

Reputation: 3975

Getting the GUID of the current user?

I want to add a user's GUID in with the information I retrieve from a user when they submit a post. How can I get the GUID?

I am using the default authentication system that comes along with an ASP.NET MVC application.

Upvotes: 12

Views: 20786

Answers (4)

live-love
live-love

Reputation: 52396

This is how to get the user guid from the user name:

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

Upvotes: 0

Anish Nair
Anish Nair

Reputation: 43

Hi there is a MVC use of the Membership example, with explanation in this blog. It shows how you can get the membership information of current logged in user.

Upvotes: 1

Martin Buberl
Martin Buberl

Reputation: 47144

If you are using the ASP.NET Membership provider:

MembershipUser user = Membership.GetUser(User.Identity.Name);
Guid guid = (Guid)user.ProviderUserKey;

or simply:

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

Upvotes: 29

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

You could simply use the username instead of hitting the database for something like this:

[Authorize]
public ActionResult PostComment()
{
    var username = User.Identity.Name;
    // Here you know the user trying to post a comment
    ...
}

Upvotes: 2

Related Questions