Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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标识-未调用自定义角色验证_C#_Asp.net_Asp.net Mvc_Validation_Asp.net Identity 3 - Fatal编程技术网

C# ASP.NET标识-未调用自定义角色验证

C# ASP.NET标识-未调用自定义角色验证,c#,asp.net,asp.net-mvc,validation,asp.net-identity-3,C#,Asp.net,Asp.net Mvc,Validation,Asp.net Identity 3,我正在尝试创建一个自定义RoleValidator对象来验证我的自定义IdentityRole。我已经创建了一个继承自RoleValidator的applicationrolevalidator类,并将其设置为我的applicationrolemager类中的RoleValidator。但是当我创建新角色时,验证函数ValidateAsync从未被调用 我曾经尝试过类似的问题,比如实现UserValidator 而这一个却无法让它工作 /// <summary> /// Custom

我正在尝试创建一个自定义
RoleValidator
对象来验证我的自定义
IdentityRole
。我已经创建了一个继承自
RoleValidator
applicationrolevalidator
类,并将其设置为我的
applicationrolemager
类中的
RoleValidator
。但是当我创建新角色时,验证函数
ValidateAsync
从未被调用

我曾经尝试过类似的问题,比如实现
UserValidator
而这一个却无法让它工作

/// <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上的Create来创建角色

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

您正在ApplicationRoleManager的Create方法中将ApplicationRoleValidator设置为ApplicationRoleManager的RoleValidator。在您发布的最后3行代码中,您正在更新ApplicationRoleManager的一个实例。此ApplicationRoleManager实例获取默认值RoleValidator

如果要新建ApplicationRoleManager实例,必须将该逻辑放入构造函数中

public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store) : base(store)
{
     RoleValidator = new ApplicationRoleValidator<ApplicationRole>(this);
}
公共应用程序角色管理器(IRoleStore存储):基本(存储)
{
RoleValidator=新应用RoleValidator(本);
}

我正试图在ASP.NET内核中通过依赖项注入实现这一点。
public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store) : base(store)
{
     RoleValidator = new ApplicationRoleValidator<ApplicationRole>(this);
}