AllowOnlyAlphanumericUserNames - how to set it? (R

2019-02-08 08:47发布

问题:

How do I set the AllowOnlyAlphanumericUserNames flag on Microsoft.AspNet.Identity.UserManager so that UserValidator will allow non-alphanumeric UserName?

回答1:

In UserManager contructor:

UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };


回答2:

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


回答3:

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


回答4:

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



回答5:

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


回答6:

You can write your own UserValidator like this. And then use it:

var userManager = new UserManager<ApplicationUser>(new CustomUserStore());
userManager.UserValidator = new CustomUserValidator<ApplicationUser>(userManager);