ASP.NET身份-设置UserValidator不会执行任何操作 [英] ASP.NET Identity - setting UserValidator does nothing
问题描述
我正在尝试在新的ASP.NET MVC 5项目(使用ASP.NET Identity 2)中为默认ApplicationUserManager
设置UserValidator
.我创建了一个非常简单的UserValidator:
I'm trying to set the UserValidator
for the default ApplicationUserManager
in a new ASP.NET MVC 5 project (using ASP.NET Identity 2). I've created a very simple UserValidator:
public class SimpleUserValidator<TUser, TKey> : IIdentityValidator<TUser> where TUser: class, IUser<TKey> where TKey : IEquatable<TKey> {
private readonly UserManager<TUser, TKey> _manager;
public SimpleUserValidator(UserManager<TUser, TKey> manager) {
_manager = manager;
}
public async Task<IdentityResult> ValidateAsync(TUser item) {
var errors = new List<string>();
if (string.IsNullOrWhiteSpace(item.UserName))
errors.Add("Username is required");
if (_manager != null) {
var otherAccount = await _manager.FindByNameAsync(item.UserName);
if (otherAccount != null && !otherAccount.Id.Equals(item.Id))
errors.Add("Select a different username. An account has already been created with this username.");
}
return errors.Any()
? IdentityResult.Failed(errors.ToArray())
: IdentityResult.Success;
}
}
我通过拨打以下电话进行设置:
I set this by calling:
manager.UserValidator = new SimpleUserValidator<ApplicationUser, int>(manager);
在ApplicationUserManager.Create()
方法之内.
问题是,这不会改变行为.我仍然收到默认的The Email field is not a valid e-mail address
消息.这不是设置验证的正确位置吗?
The problem is, this doesn't change the behavior. I still get the default The Email field is not a valid e-mail address
message. Is this not the correct place to set that validation?
推荐答案
我对此问题有类似的问题,这里的答案不太正确,所以我在这里添加了我的内容.
I had a similar issue to this question and the answer here is not quite right so I am adding mine here to share.
此问题是由在创建"功能中设置UserValidator
引起的.由于我正在新建ApplicationUserManager
的实例,因此使用了默认的验证器.将应用程序验证设置移动到构造函数即可解决此问题.我认为Create函数应该仅具有选项特定的设置.
The issue was caused by setting the UserValidator
inside the Create function. Since I was newing an instance of the ApplicationUserManager
the default validator was being used. Moving the application validation setting to the constructor solved the problem. The Create function should only have the option specific settings I think.
public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationUserManager"/> class.
/// </summary>
/// <param name="store"></param>
public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
: base(store)
{
// Configure validation logic for usernames
UserValidator = new ApplicationUserValidator<ApplicationUser>(this)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = false
};
// Configure validation logic for passwords
PasswordValidator = new PasswordValidator
{
RequiredLength = 8,
RequireNonLetterOrDigit = false,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true
};
}
/// <summary>
/// Creates the specified options.
/// </summary>
/// <param name="options">The options.</param>
/// <param name="context">The context.</param>
/// <returns></returns>
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<MyContext>()));
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
或者,您可以删除所有new ApplicationUserManager
实例,然后调用创建".但这很难在不使构造函数私有的情况下使用您的代码强制他人使用
Alternatively you could remove all instances of new ApplicationUserManager
, and call Create instead. But that is dificult to enforce on others using your code without making the constructor private
将构造函数设为私有将使播种用户表变得困难,因为IOwinContext
在播种功能中不可用.同样,您将无法通过在构造过程中通过模拟商店来对ApplicationUserManager
进行单元测试.
Making the constructor private would make seeding the user table difficult because the IOwinContext
is not available in the seeding function. Also you would not be able to unit test your ApplicationUserManager
by passing a mocked store during construction.
这篇关于ASP.NET身份-设置UserValidator不会执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!