Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET标识-设置UserValidator不起任何作用_C#_Asp.net_Asp.net Mvc_Asp.net Identity 2 - Fatal编程技术网

C# ASP.NET标识-设置UserValidator不起任何作用

C# ASP.NET标识-设置UserValidator不起任何作用,c#,asp.net,asp.net-mvc,asp.net-identity-2,C#,Asp.net,Asp.net Mvc,Asp.net Identity 2,我正在尝试在新的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 :

我正在尝试在新的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;
    }
}
公共类SimpleUserValidator:IIdentityValidator其中TUser:class,IUser其中TKey:IEquatable{
私有只读用户管理器\u管理器;
公共SimpleUserValidator(用户管理器){
_经理=经理;
}
公共异步任务ValidateAsync(TUser项){
var errors=新列表();
if(string.IsNullOrWhiteSpace(item.UserName))
错误。添加(“需要用户名”);
如果(_manager!=null){
var otherAccount=wait_manager.FindByNameAsync(item.UserName);
if(otherAccount!=null&&!otherAccount.Id.Equals(item.Id))
错误。添加(“选择其他用户名。已使用此用户名创建了帐户。”);
}
返回错误。Any()
?IdentityResult.Failed(errors.ToArray())
:IdentityResult.Success;
}
}
我通过调用:

manager.UserValidator = new SimpleUserValidator<ApplicationUser, int>(manager);
manager.UserValidator=新的SimpleUserValidator(管理器);
ApplicationUserManager.Create()方法中


问题是,这不会改变行为。我仍然得到默认的
电子邮件字段不是有效的电子邮件地址
消息。这不是设置验证的正确位置吗?

我找到了。这应该是显而易见的


UserValidator
不是从新模板进行验证所需更改的唯一内容。您还必须更改所有相关的视图模型。想想看。

我对这个问题有一个类似的问题,这里的答案不太正确,所以我将我的问题添加到这里与大家分享

该问题是由于在Create函数中设置了
UserValidator
引起的。因为我正在更新
ApplicationUserManager
的一个实例,所以使用了默认的验证器。将应用程序验证设置移动到构造函数解决了这个问题。我认为Create函数应该只有特定于选项的设置

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;
    }
}
公共类应用程序管理员:用户管理员
{
/// 
///初始化类的新实例。
/// 
/// 
公共应用程序服务器管理器(IUserStore存储)
:基地(商店)
{
//为用户名配置验证逻辑
UserValidator=新的ApplicationServalidator(此)
{
AllowOnlyAlphanumericUserNames=true,
RequireUniqueEmail=false
};
//配置密码的验证逻辑
PasswordValidator=新的PasswordValidator
{
所需长度=8,
RequiredOnletterDigit=false,
RequireDigit=true,
RequireLowercase=true,
RequireUppercase=true
};
}
/// 
///创建指定的选项。
/// 
///选项。
///上下文。
/// 
公共静态应用程序SerManager创建(IdentityFactoryOptions选项,IOwinContext上下文)
{
var manager=newapplicationUserManager(newapplicationUserStore(context.Get());
var dataProtectionProvider=options.dataProtectionProvider;
if(dataProtectionProvider!=null)
{
manager.UserTokenProvider=newdataprotectortokenprovider(dataProtectionProvider.Create(“ASP.NET标识”);
}
退货经理;
}
}
或者您可以删除
新ApplicationUserManager
的所有实例,并调用Create。但是,在不使构造函数私有的情况下,对使用您的代码的其他人强制执行这一点是很困难的


将构造函数设为私有将使设定用户表种子变得困难,因为设定种子功能中没有
IOwinContext
。此外,在构建过程中,通过传递模拟存储,您将无法对您的
应用程序SerManager
进行单元测试。

您是否尝试单步执行/调试此方法(例如,在其中设置断点)?有人打电话吗?没有。这就是让我困惑的部分原因。