18

How can I use email instead of user name in the new ASP.NET identity system?

I tried to change the RegisterViewModel class:

[Display(Name = "Email address")]
[Required(ErrorMessage = "The email address is required")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string UserName { get; set; }

but when I enter an mail adress I am getting the error:

User name fsdfsd@fsdfsd.de is invalid, can only contain letters or digits.
Askolein
  • 3,250
  • 3
  • 28
  • 40
daniel
  • 34,281
  • 39
  • 104
  • 158

1 Answers1

22

You have 2 options to solve it by either turning off that validator, or create your own UserValidator.

You could turn it off like this:

UserManager.UserValidator = new UserValidator<TUser>(UserManager) 
                                    { 
                                       AllowOnlyAlphanumericUserNames = false 
                                    };
meda
  • 45,103
  • 14
  • 92
  • 122
  • 4
    I suggest creating a custom UserManager (e.g. AppUserManager : UserManager) that sets the UserValidator of itself in this way when instantiated. I do this so that I don't have to remember setting the UserValidator when newing up a UserManager class, and only have to remember to use my custom AppUserManager class. – Jeremy Cook Dec 06 '13 at 16:58