【问题标题】:how to use UserId instead of user name in authentication ( Web Forms asp.net)?如何在身份验证(Web Forms asp.net)中使用 UserId 而不是用户名?
【发布时间】:2014-07-28 07:49:30
【问题描述】:

我有 Visual Studio 2013 Ultimate。我已经使用 .Net Framework 4 创建了 ASP.NET Web 窗体应用程序(不是 MVC)
用户主键是唯一标识符(GUID), 我在默认的Login.aspx、Register.aspx等表格中找到了。有些代码使用 User.Identity.Name 而不是主键 UserId :

 Dim hasLocalPassword = OpenAuth.HasLocalPassword(User.Identity.Name)
 Dim accounts As IEnumerable(Of OpenAuthAccountData) = OpenAuth.GetAccountsForUser(User.Identity.Name)
 Dim result As SetPasswordResult = OpenAuth.AddLocalPassword(User.Identity.Name, password.Text)

如何将此“User.Identity.Name”更改为用户的主键 GUID,因为两个以上的用户可以有相同的名称?

【问题讨论】:

    标签: asp.net .net web webforms


    【解决方案1】:

    如果您使用的是 AspNet 会员提供商:

    MembershipUser user = Membership.GetUser(User.Identity.Name);
    Guid guid = (Guid)user.ProviderUserKey;
    

    如果是 Oauth,您必须这样做。

    You should add `[InitializeSimpleMembership]` on top of controller class if you use another controller than AccountController. 
    
    WebSecurity.GetUserId(User.Identity.Name);
    

    还要检查这个答案: MVC Simplemembership I cannot get the userID after logging a user in via OAuth

    【讨论】:

    • 我有两个同名用户。怎么做。此 AspNet 成员资格不允许其他同名用户??
    • 有什么方法可以使用 UserId 作为主键,并且想用 SAME NAME 创建用户??
    • 我不知道为什么微软为会员默认创建了这个(User.Identity.Name),因为可以有更多同名用户..
    • 任何时候它都会根据当前登录的用户有价值,不要在你的应用程序中允许重复的用户名。名字和姓氏可以相同,但用户名必须是唯一的。
    猜你喜欢
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 2012-07-28
    • 2019-09-27
    相关资源
    最近更新 更多