8

My current Asp.Net MVC 5 project mandates Email address for UserName. Now I want to upgrade ASPNet Identity v1.0 to v2.0 to leverage all its new features (see here).

However, ASPNet Identity v2.0 adds Email as a separate column to the Users table and adds a corresponding property to the IdentityUser class.

I don't want to duplicate UserName into this new Email column. How can I map this Email Property of IdentityUser to use existing UserName column & property? Is it possible to ignore this Email property and skip adding the column in the Users table? Has anybody tried this?

Please share.

Update

This is the identity 2.0 limitation. We cannot ignore Email property or leave it Null. Some of the Identity functionality will not work. :(

Santosh
  • 2,430
  • 5
  • 33
  • 47
  • 1
    I am able to make this work by implementing my own User class that just inherits from IUser, and implementing UserManger, UserStore. – user210757 May 18 '15 at 17:44
  • @santhosh can you please look this question and tel me the suggestion ? – SivaRajini May 02 '16 at 07:37
  • http://stackoverflow.com/questions/36929767/asp-net-identity-entity-framework-database-first-approach-with-own-table-definti – SivaRajini May 02 '16 at 07:37

2 Answers2

4

You can try one of these:

  1. Try to ignore it by either overriding Email property in your User class and unmapping it or using fluent API.

    public class ApplicationUser : IdentityUser
    {
      // ....
    
      [NotMapped]
      public override string Email { get; set; }
    }
    

    or

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ApplicationUser>().Ignore(u => u.Email);
    }
    
  2. When you register your user just make sure that you populate Email with your UserName

    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            // ...
        }
    }
    

Of course, you can always ignore Email column if you're not going to use it, since it allows NULLs, it'll just be sitting in your AspNetUsers table with bunch of NULLs, not the best approach but remember that by ignoring it you might lose new features that ASP.NET Identity 2 might offer that you might want to use.

NOTE However I'm not sure if option number 1 will work on Email property since it's probably used all over the place in new Identity code. Worth a try though. I know that's how you can get rid of other columns if you don't need them. I personally happen to use new Email property/column so I haven't tried it.

Not sure if it helps you, but thought I'd share it just in case.

dima
  • 1,181
  • 1
  • 9
  • 20
  • 2
    I had already tried your option 1 which did not work. (InvalidOperationException: The property 'Email' is not a declared property on type 'ApplicationUser'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.) Your option 2 is already shown in the samples (check nuget.org/packages/Microsoft.AspNet.Identity.Samples). I would prefer not to have Email column in the Users table and so the question! – Santosh Apr 16 '14 at 04:51
  • @Santosh Point is that lots of Identity code is strongly-coupled with the `email`-column thus removing it may break more than you realise - certainly that's not what you want since we're talking about security. And let's be honest, one unused NULL column will never be the bottleneck in your app performance ;-) – M. Mimpen Sep 26 '14 at 06:49
2

I have the same problem, and the way that I resolved it was that the email address was the same as the username when creating a user:

var newUser = new ApplicationUser()
{
    UserName = email,
    Email = email,
};

However, if you try to create an account with a duplicate username, you will get 2 validation errors, one for the username field, and one for the email address.

To get around that, allow email addresses to not be unique (they will still be unique though as your usernames are unique) by editing the identityconfig.cs file:

manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = false
            };
Evonet
  • 3,600
  • 4
  • 37
  • 83