我正在尝试创建一个自定义RoleValidator
对象以验证我的自定义IdentityRole
。我创建了一个从ApplicaitonRoleValidator
继承的RoleValidator
类,并将其设置为我的RoleValidator
类中的ApplicationRoleManager
。但是,当我创建新角色时,永远不会调用验证功能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行代码中,您正在更新ApplicationRoleManager的实例。 ApplicationRoleManager的此实例获取默认的RoleValidator。
如果要新建ApplicationRoleManager的实例,则必须将该逻辑放入构造函数中
public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store) : base(store)
{
RoleValidator = new ApplicationRoleValidator<ApplicationRole>(this);
}
关于c# - ASP.NET身份-未调用自定义角色验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38217501/