Pac-Man
Pac-Man

Reputation: 187

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);

Upvotes: 0

Views: 771

Answers (2)

TerribleDev
TerribleDev

Reputation: 2245

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
        };
    }
}

Upvotes: 2

Cyr
Cyr

Reputation: 501

I have had the same problem. With the solution proposed by @tparnell (which is large used) nothing changes and the validation continue to fail.

I have solved with a new declaration inside the AccountController:

userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
userManager.UserValidator = new UserValidator<ApplicationUser>(userManager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

Upvotes: 0

Related Questions