How do I get the ID of the current logged in user?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 09:41:33

问题


I'm working on a sample project built from the ASP.NET Web Application template (web forms, not MVC). After a user logs in, I want to grab the user's ID and store it in a session variable so I can pass it between pages. How do I get the user's ID from the ASP.NET membership provider? By user's ID, I'm referring to the GUID in the membership database. Is there a method to get the user's ID?

I'm using the default database schema and have it deployed to my server.

Thanks!


回答1:


Try this:

MembershipUser membershipUser = Membership.GetUser();
string UserID = membershipUser.ProviderUserKey.ToString();



回答2:


You don't need passing user id around. Currently authenticated user is stored in HttpContext.Current.User.




回答3:


you can define a static method in a class and use it:

public static Guid getCurrentUserGUID(){
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
        MembershipUser myObject;
        myObject = Membership.GetUser(HttpContext.Current.User.Identity.Name);
        return (Guid)myObject.ProviderUserKey;
    }
    return Guid.Empty;       
}


来源:https://stackoverflow.com/questions/5706785/how-do-i-get-the-id-of-the-current-logged-in-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!