AspNet Identity 2.0 Email Replay and UserName

My current Asp.Net MVC 5 project sets an email address for UserName. Now I want to upgrade ASPNet Identity v1.0 to v2.0 to use all my new features ( see here ).

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

I do not want to duplicate UserName in this new E-mail column. How can I map this IdentityUser email property to use the existing column and the UserName property? Can I ignore this email property and skip adding a column to the Users table? Has anyone tried this?

Please share.

Update

This is a limitation of 2.0. We cannot ignore the email property or leave it Null. Some of the Identity features will not work. :(

+7
asp.net-mvc entity-framework asp.net-identity
source share
2 answers

You can try one of them:

  • Try to ignore this by either overriding the Email property in your User class and canceling it, or using the free 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); } 
  • When you register your user, just make sure that you fill in the 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 the Email column if you are not going to use it, since it is NULL, it will just sit in your AspNetUsers table with a NULL bundle, and not the best approach, but remember that by ignoring it, you may lose new features that ASP.NET Identity 2 may offer, which you might want to use.

NOTE However, I am not sure if option number 1 will work with Email , as it was probably used everywhere in the new Identity code. However, it is worth a try. I know that you can get rid of other columns if you do not need them. I personally accidentally used the new Email property / column, so I have not tried it.

I'm not sure that this will help you, but I thought that I would share it just in case.

+3
source share

I have the same problem and I decided that the email address was the same as the username when creating the user:

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

However, if you try to create an account with a double username, you will receive two validation errors, one for the username field and one for the email address.

To get around this, let the email addresses not be unique (they will still be unique, though, since your usernames are unique) by editing the identityconfig.cs file:

 manager.UserValidator = new UserValidator<ApplicationUser>(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = false }; 
+1
source share

All Articles