开发者

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

I'm working on a sample project built from the ASP.NET Web Application template (web forms, not MVC). Af开发者_开发知识库ter 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!


Try this:

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


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


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;       
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜