AllowOnlyAlphanumericUserNames - how to set it? (R

2019-02-08 08:57发布

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

6条回答
女痞
2楼-- · 2019-02-08 09:30

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

查看更多
Ridiculous、
3楼-- · 2019-02-08 09:35

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);
查看更多
走好不送
4楼-- · 2019-02-08 09:40

In UserManager contructor:

UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
查看更多
一夜七次
5楼-- · 2019-02-08 09:41

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楼-- · 2019-02-08 09:42

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; }
}
查看更多
相关推荐>>
7楼-- · 2019-02-08 09:48

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; }
查看更多
登录 后发表回答