ASP.NET 身份 - 未调用自定义角色验证

ASP.NET Identity - Custom role validation not being called

我正在尝试创建一个自定义 RoleValidator 对象来验证我的自定义 IdentityRole。我创建了一个继承自 RoleValidatorApplicaitonRoleValidator class,并将其设置为我的 ApplicationRoleManager class 中的 RoleValidator。但是当我创建新角色时,永远不会调用验证函数 ValidateAsync

我曾尝试查看类似的问题来实现 UserValidator,例如 How can customize Asp.net Identity 2 username already taken validation message? 还有这个 ASP.NET Identity - setting UserValidator does nothing 但无法正常工作。

/// <summary>
/// Custom role validator, used to validate new instances of ApplicationRole that are added to the system.
/// </summary>
/// <typeparam name="TRole">The type of the role.</typeparam>
public class ApplicationRoleValidator<TRole> : RoleValidator<TRole> where TRole : ApplicationRole
{
    private RoleManager<TRole, string> Manager { get; set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="ApplicationRoleValidator" /> class.
    /// </summary>
    /// <param name="manager">The manager.</param>
    public ApplicationRoleValidator(RoleManager<TRole, string> manager) : base(manager)
    {
        Manager = manager;
    }

    /// <summary>
    /// Validates a role before saving.
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    /// <exception cref="System.ArgumentNullException">item</exception>
    public override async Task<IdentityResult> ValidateAsync(TRole item)
    {
        if (item == null)//<= break point here never reached.
        {
            throw new ArgumentNullException(nameof(item));
        }

        var rslt = base.ValidateAsync(item);
        if (rslt.Result.Errors.Any())
        {//return error if found
            return IdentityResult.Failed(rslt.Result.Errors.ToArray());
        }

        var errors = new List<string>();
        //validate the min num of members
        if (role.MinimumNumberOfMembers < 0)
        {
            errors.Add(string.Format(CultureInfo.CurrentCulture, "最小数は0以上でなければなりません。"));
        }

        return errors.Count > 0 ? IdentityResult.Failed(errors.ToArray()) : IdentityResult.Success;
    }
}

ApplicationRoleManager 其中自定义 RoleValidator 是在创建期间设置的。我可以打破那条线,所以我知道它正在被调用。

public class ApplicationRoleManager : RoleManager<ApplicationRole, string>
{
    /// <summary>
    /// Initializes a new instance of the <see cref="ApplicationRoleManager"/> class.
    /// </summary>
    /// <param name="store"></param>
    public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
        : base(store)
    {
    }
    /// <summary>
    /// Creates the specified options.
    /// </summary>
    /// <param name="options">The options.</param>
    /// <param name="context">The context.</param>
    /// <returns></returns>
    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        var manager = new ApplicationRoleManager(new ApplicationRoleStore(context.Get<MyContext>()));
        manager.RoleValidator = new ApplicationRoleValidator<ApplicationRole>(manager);
        return manager;
    }
}
public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
    public bool IsSystemGroup { get; set; }
    public string Description { get; set; } = "";
    public int MinimumNumberOfMembers { get; set; }
}

public class ApplicationRoleStore : RoleStore<ApplicationRole, string, ApplicationUserRole>
{
    public ApplicationRoleStore(MyContext context)
        : base(context)
    {
    }
}

正在通过调用 ApplicationRoleManager 上的 Create 创建角色

var store = new ApplicationRoleStore(new MyContext());
var manager = new ApplicationRoleManager(store);
manager.Create(group);

您在 ApplicationRoleManager 的 Create 方法中将 ApplicationRoleValidator 设置为 ApplicationRoleManager 的 RoleValidator。在您发布的最后 3 行代码中,您是 new ApplicationRoleManager 的一个实例。此 ApplicationRoleManager 实例获取 default RoleValidator。

如果你想新建一个 ApplicationRoleManager 的实例,你必须将该逻辑放在构造函数中

public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store) : base(store)
{
     RoleValidator = new ApplicationRoleValidator<ApplicationRole>(this);
}