【问题标题】:ASP.NET Identity v2.0 IdentityUser properties missing缺少 ASP.NET Identity v2.0 IdentityUser 属性
【发布时间】:2014-06-25 14:44:08
【问题描述】:

我在之前的应用程序中一直在使用 ASP.NET 标识 1.0,并且通过遵循像 this 这样的简单教程,我确实设法在我的用户类中实现了自定义属性:

public class CustomUser : IdentityUser
{
    public string ContactName { get; set; }
    public string CompanyName { get; set; }
    public string Address { get; set; }
    public string Postcode { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Fax { get; set; }
    public string Email { get; set; }
    public bool Subscription { get; set; }
}

然后在 AccountController 的 register 方法中,我可以将 ViewModel 映射到 CustomUser 并调用 UserManager.CreateAsync 方法,传递我的用户和密码字符串并在系统中注册新用户。

当我尝试在 Identity v2.0 中做同样的事情时,它不再可能了。我注意到 IdentityUser 没有以前可用的 UserName、Claims、Id 和其他属性。另外,我注意到必须在新版本的 IdentityUser 中声明 Email 字段,因为我不能再在我的自定义用户中设置它。

我相信所有这些属性都必须以某种方式抽象,但不知道如何抽象。我的问题是如何设置我的 CustomUser 的 UserName 和 Email 属性以立即发送它进行注册?

【问题讨论】:

    标签: asp.net identity asp.net-identity user-management usermanager


    【解决方案1】:

    我最近一直在研究这个问题,并偶然发现了这个博客,这很好,但更重要的是,有一个链接可以安装一个完整实现的示例......

    http://blogs.msdn.com/b/webdev/archive/2014/03/20/test-announcing-rtm-of-asp-net-identity-2-0-0.aspx

    基本上你是对的,有一个抽象允许更改密钥的类型,但电子邮件和用户名等仍然存在,只是处于较低级别。如果您继续查看您所继承的定义,您会找到它们。遇到这个问题后,我用整数键实现了一个......

    Identity change GUID to int

    这是他们在 Identity 2.0 中的位置

    namespace Microsoft.AspNet.Identity.EntityFramework
    {
      public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
        where TLogin : Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>
        where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
        where TClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>
      {
        // Summary:
        //     Constructor
        public IdentityUser();
    
        // Summary:
        //     Used to record failures for the purposes of lockout
        public virtual int AccessFailedCount { get; set; }
        //
        // Summary:
        //     Navigation property for user claims
        public virtual ICollection<TClaim> Claims { get; }
        //
        // Summary:
        //     Email
        public virtual string Email { get; set; }
        //
        // Summary:
        //     True if the email is confirmed, default is false
        public virtual bool EmailConfirmed { get; set; }
        //
        // Summary:
        //     User ID (Primary Key)
        public virtual TKey Id { get; set; }
        //
        // Summary:
        //     Is lockout enabled for this user
        public virtual bool LockoutEnabled { get; set; }
        //
        // Summary:
        //     DateTime in UTC when lockout ends, any time in the past is considered not
        //     locked out.
        public virtual DateTime? LockoutEndDateUtc { get; set; }
        //
        // Summary:
        //     Navigation property for user logins
        public virtual ICollection<TLogin> Logins { get; }
        //
        // Summary:
        //     The salted/hashed form of the user password
        public virtual string PasswordHash { get; set; }
        //
        // Summary:
        //     PhoneNumber for the user
        public virtual string PhoneNumber { get; set; }
        //
        // Summary:
        //     True if the phone number is confirmed, default is false
        public virtual bool PhoneNumberConfirmed { get; set; }
        //
        // Summary:
        //     Navigation property for user roles
        public virtual ICollection<TRole> Roles { get; }
        //
        // Summary:
        //     A random value that should change whenever a users credentials have changed
        //     (password changed, login removed)
        public virtual string SecurityStamp { get; set; }
        //
        // Summary:
        //     Is two factor enabled for the user
        public virtual bool TwoFactorEnabled { get; set; }
        //
        // Summary:
        //     User name
        public virtual string UserName { get; set; }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-17
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多