Joh*_*mer 16 asp.net identity visual-studio-2013 asp.net-identity
如何在Microsoft.AspNet.Identity.UserManager上设置AllowOnlyAlphanumericUserNames标志,以便UserValidator将允许非字母数字用户名?
Joh*_*mer 12
在UserManager构造函数中:
UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
Run Code Online (Sandbox Code Playgroud)
另一种方法:
[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; }
}
Run Code Online (Sandbox Code Playgroud)
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 };
}
}
Run Code Online (Sandbox Code Playgroud)
以下是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; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4320 次 |
| 最近记录: |