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
  • 2,992
  • 2
  • 26
  • 36
daniel
  • 32,027
  • 36
  • 92
  • 151

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
  • 44,540
  • 14
  • 88
  • 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