![](/img/trans.png)
[英]Custom UserConfirmation not being called in Asp.Net Core 5 Identity
[英]ASP.NET Identity - Custom role validation not being called
我正在嘗試創建一個自定義RoleValidator
對象來驗證我的自定義IdentityRole
。 我已經創建了一個ApplicaitonRoleValidator
從繼承類RoleValidator
並將此作為RoleValidator
我ApplicationRoleManager
類。 但是當我創建新角色時,永遠不會調用驗證函數ValidateAsync
。
我試過看類似的問題實現UserValidator
如何自定義Asp.net Identity 2用戶名已經采取驗證消息? 而這一個ASP.NET身份 - 設置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
在創建期間設置自定義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行,你是newing ApplicationRoleManager的一個實例。 ApplicationRoleManager的此實例獲取默認的 RoleValidator。
如果要創建ApplicationRoleManager的實例,則必須將該邏輯放在構造函數中
public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store) : base(store)
{
RoleValidator = new ApplicationRoleValidator<ApplicationRole>(this);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.