Search code examples
c#asp.net-identity

Cannot create new users when submitted username contains dash symbol


Help me please, I'm trying to create a new user user using class IdentityUser(), but when I add in my username the dash(-) symbol I get an error. How can I fix this?

var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore);

var user = new IdentityUser() { UserName = aspNetUsers.Email };
IdentityResult result = manager.Create(user, aspNetUsers.PasswordHash);

Solution

  • The user manager class has a called UserValidator, in that class there is a property you need to alter:

    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store)
        {
            this.UserValidator = new UserValidator<ApplicationUser>(this)
            {
                AllowOnlyAlphanumericUserNames = false
            };
        }
    }