Search code examples
asp.netidentityasp.net-identityuser-managementusermanager

ASP.NET Identity v2.0 IdentityUser properties missing


I've been using ASP.NET identity 1.0 in my previous application and by following simple tutorials like this did manage to implement custom properties into my user class:

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

So then in AccountController in register method I could map my ViewModel onto CustomUser and call UserManager.CreateAsync method, pass my user and password string and register new user in the system.

When I try to do the same in Identity v2.0 it's not possible anymore. I've noticed that IdentityUser does not have UserName, Claims, Id and other properties that were available before. Also, I've noticed that Email field must be declared somewhere in the new version of IdentityUser since I'm not allowed to set it in my custom user any more.

I believe that all of those properties must have been abstracted in some way, but cannot figure out how. My question is how can I set UserName and Email property of my CustomUser to send it for registration now?


Solution

  • I've just been looking into this a lot recently and came accross this blog which is good but more importantly has a link to installing a sample with a full implementation...

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

    Basically you're right, there's been an abstration to allow for a change in type to the key but the email and UserName etc are still there, just at a lower level. If you keep looking at the definitions of what you're inheriting from, you'll find them. I've implemented one with an integer key after coming accross this question...

    Identity change GUID to int

    Here's where they are in 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; }
      }
    }