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

时间:2014-05-08 09:29:38

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

我在之前的应用程序中一直使用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中声明电子邮件字段,因为我不允许在我的自定义用户中设置它。

我相信所有这些属性必须以某种方式被抽象,但无法弄清楚如何。我的问题是如何设置我的CustomUser的UserName和Email属性以便立即发送给它?

1 个答案:

答案 0 :(得分: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; }
  }
}