【问题标题】:ASP.NET Identity - setting UserValidator does nothingASP.NET Identity - 设置 UserValidator 什么都不做
【发布时间】: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


【解决方案1】:

我也有类似的问题,这里的答案不太正确,所以我在这里添加我的分享。

问题是由在 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;
    }
}

或者您可以删除new ApplicationUserManager 的所有实例,然后调用 Create。但是在不将构造函数设为私有的情况下,很难对使用您的代码的其他人强制执行

将构造函数设为私有会使为用户表做种变得困难,因为IOwinContext 在播种函数中不可用。此外,您将无法通过在构建期间传递模拟商店来对您的 ApplicationUserManager 进行单元测试。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 2017-11-23
      • 2015-03-17
      • 2017-11-06
      • 2020-05-13
      • 1970-01-01
      相关资源
      最近更新 更多