【发布时间】:2016-07-11 09:32:29
【问题描述】:
我正在尝试在新的 ASP.NET MVC 5 项目(使用 ASP.NET Identity 2)中为默认的 ApplicationUserManager 设置 UserValidator。我创建了一个非常简单的 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;
}
}
我通过调用来设置:
manager.UserValidator = new SimpleUserValidator<ApplicationUser, int>(manager);
在ApplicationUserManager.Create() 方法中。
问题是,这不会改变行为。我仍然收到默认的The Email field is not a valid e-mail address 消息。这不是设置验证的正确位置吗?
【问题讨论】:
-
您是否尝试过单步调试/调试此方法(例如,在其中设置断点)?它被调用了吗?
-
不,不是。这是让我感到困惑的部分原因。
标签: c# asp.net asp.net-mvc asp.net-identity-2