16

如何在 Microsoft.AspNet.Identity.UserManager 上设置 AllowOnlyAlphanumericUserNames 标志,以便 UserValidator 允许非字母数字用户名?

4

6 回答 6

12

在 UserManager 构造函数中:

UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
于 2013-10-18T22:58:52.380 回答
8

还有另一种方法:

[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; }
}
于 2013-11-08T21:05:04.983 回答
6

约翰的回答是对的,我用他的回答允许电子邮件作为用户名(默认情况下不起作用)

请投票/接受约翰的回答。
这是一些我使用自定义 UserManager 的代码来让事情正常工作
(这样在其他地方也减少了重复)

public class MyUserManager : UserManager<ApplicationUser>
{
    public MyUserManager(DbContext db)
        : base(new UserStore<ApplicationUser>(db))
    {
        this.UserValidator = UserValidator = new UserValidator<ApplicationUser>(this) 
          { AllowOnlyAlphanumericUserNames = false };
    }
}

这是 AccountController 构造函数代码现在的样子:

[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; }
于 2013-11-04T18:52:07.630 回答
2

从 ASP.NET Identity 3.0(目前在 RC 中)开始,现在将其配置为用户的一个选项。

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

}

与 Gist 相同的代码:https ://gist.github.com/pollax/4449ce7cf47bde6b3a95

于 2016-02-28T11:59:07.803 回答
1

另一种做事方式

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,
            };
于 2014-08-08T16:47:48.450 回答
0

您可以像这样编写自己的 UserValidator 。然后使用它:

var userManager = new UserManager<ApplicationUser>(new CustomUserStore());
userManager.UserValidator = new CustomUserValidator<ApplicationUser>(userManager);
于 2014-04-11T16:22:47.543 回答