How do I set the AllowOnlyAlphanumericUserNames flag on Microsoft.AspNet.Identity.UserManager so that UserValidator will allow non-alphanumeric UserName?
Asked
Active
Viewed 5,457 times
6 Answers
12
In UserManager contructor:
UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };

John Palmer
- 1,032
- 1
- 9
- 23
-
I don't see where to put this. Can you please elaborate? – Garrett Fogerlie Nov 01 '13 at 15:43
-
2See mine and Kevin Radcliffe's answers for complete examples. – angularsen Nov 08 '13 at 21:06
9
Yet another way of doing it:
[Authorize]
public class AccountController : Controller
{
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
// Start of new code
UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager)
{
AllowOnlyAlphanumericUserNames = false,
};
// End of new code
}
public UserManager<ApplicationUser> UserManager { get; private set; }
}

angularsen
- 8,160
- 1
- 69
- 83
6
John's answer is right, I used his answer to allow email as username (Wasn't working by default)
Please upvote/accept John's answer.
Here is some code where I used a custom UserManager" to get things working
(This way there's less repeating elsewhere too)
public class MyUserManager : UserManager<ApplicationUser>
{
public MyUserManager(DbContext db)
: base(new UserStore<ApplicationUser>(db))
{
this.UserValidator = UserValidator = new UserValidator<ApplicationUser>(this)
{ AllowOnlyAlphanumericUserNames = false };
}
}
And here's what the AccountController Constructor code looks like now:
[Authorize]
public class AccountController : Controller
{
public AccountController()
: this(new MyUserManager(new AppContext()))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
}
public UserManager<ApplicationUser> UserManager { get; private set; }

Kevin Radcliffe
- 991
- 1
- 11
- 21
2
As of ASP.NET Identity 3.0 (currently in RC), this is now configured as an option on the user.
public void ConfigureServices(IServiceCollection services)
{
// (Rest of code removed)
// Note the + added to the string of allowed user name characters
services.AddIdentity<ApplicationUser, IdentityRole>(o => o.User.AllowedUserNameCharacters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -._@+")
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
}
Same code as a Gist: https://gist.github.com/pollax/4449ce7cf47bde6b3a95

Anton
- 1,346
- 11
- 31
1
another way of doing
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};

Nick Kahn
- 19,652
- 91
- 275
- 406