首页 文章

缺少ASP.NET Identity v2.0 IdentityUser属性

提问于
浏览
1

我一直在我之前的应用程序中使用ASP.NET identity 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属性以立即发送给它?

1 回答

  • 2

    我最近刚刚对这个问题进行了大量研究并且在这篇博客中发表了很好的内容,但更重要的是有一个链接来安装完整实现的示例...

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

    基本上你是对的,有一个abstration允许改变键的类型,但电子邮件和UserName等仍然存在,只是在较低的水平 . 如果你一直在查看你继承的定义,你会发现它们 . 在遇到这个问题后,我用一个整数键实现了一个...

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

相关问题