Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 559
irl_irl Avatar asked Jan 22 '11 17:01

irl_irl


3 Answers

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;
like image 105
Martin Buberl Avatar answered Oct 23 '22 04:10

Martin Buberl


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
    ...
}
like image 30
Darin Dimitrov Avatar answered Oct 23 '22 05:10

Darin Dimitrov


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.

like image 1
Anish Nair Avatar answered Oct 23 '22 05:10

Anish Nair