How can I access UserId in ASP.NET Membership without using Membership.GetUser()?

前端 未结 10 484
孤街浪徒
孤街浪徒 2020-12-08 16:07

How can I access UserId in ASP.NET Membership without using Membership.GetUser(username) in ASP.NET Web Application Project?

Can User

相关标签:
10条回答
  • 2020-12-08 16:57
    public string GetUserID()
                {
                    MembershipUser _User;
                    string _UserId = "";
                    _User = Membership.GetUser();
                    Guid UserId = (Guid)_User.ProviderUserKey;
                    return _UserId = UserId.ToString();
                }
    
    0 讨论(0)
  • 2020-12-08 16:59

    I decided to write authentication of users users on my own (very simple but it works) and I should done this long time ago.

    My original question was about UserId and it is not available from:

    System.Web.HttpContext.Current.User.Identity.Name
    
    0 讨论(0)
  • 2020-12-08 17:00

    Try the following:

    Membership.GetUser().ProviderUserKey
    
    0 讨论(0)
  • 2020-12-08 17:06

    You have two options here:

    1) Use username as the primary key for your user data table i.e:

    select * from [dbo.User] where Username = 'andrew.myhre'
    

    2) Add UserID to the profile.

    There are pros and cons to each method. Personally I prefer the first, because it means I don't necessarily need to set up the out-of-the-box profile provider, and I prefer to enforce unique usernames in my systems anyway.

    0 讨论(0)
提交回复
热议问题