AllowOnlyAlphanumericUserNames - 如何设置? (RC到RTM破坏变化)ASP.NET身份

时间:2013-10-18 21:50:43

标签: asp.net identity visual-studio-2013 asp.net-identity

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

6 个答案:

答案 0 :(得分:12)

在UserManager构造函数中:

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

答案 1 :(得分: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; }
}

答案 2 :(得分:6)

John的答案是对的,我用他的答案允许电子邮件作为用户名(默认不工作)

请upvote /接受John的回答 这是一些代码,我使用自定义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; }

答案 3 :(得分:2)

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

mycoolapp://http://xxxxxx.co/#/guest/aaaa

}

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

答案 4 :(得分: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,
            };

答案 5 :(得分:0)

您可以编写自己的UserValidator,如this。然后使用它:

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