简体   繁体   中英

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.

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;

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
    ...
}

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.

这是如何从用户名获取用户guid:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM