理想的には、ユーザーが作成しようとしているパスワードにユーザー名の順列が含まれていないことを含め、一連の追加チェックを行うカスタム パスワード検証に取り組んでいます。
私たちは Identity Framework を使用しています。当初の意図は、次のように IIdentityValidator を拡張することでした。
public class StrongPasswordValidator : IIdentityValidator<string>
{
public int MinimumLength { get; set; }
public int MaximumLength { get; set; }
public void CustomPasswordValidator(int minimumLength, int maximumLength)
{
this.MinimumLength = minimumLength;
this.MaximumLength = maximumLength;
}
public Task<IdentityResult> ValidateAsync(string item)
{
if (item.Length < MinimumLength)
{
return Task.FromResult(IdentityResult.Failed("Password must be a minimum of " + MinimumLength + " characters."));
}
if (item.Length > MaximumLength)
{
return Task.FromResult(IdentityResult.Failed("Password must be a maximum of " + MaximumLength + " characters."));
}
return Task.FromResult(IdentityResult.Success);
}
}
ただし、UserManager ごとに引数として文字列しか使用できません。ここでもユーザー名/カスタムオブジェクトを取得する方法はありますか、または情報を永続化する前にパスワード文字列をユーザー名と比較する方法はありますか?
編集
コンテキストとして、これはパスワードバリデーターの設定方法と使用方法です。
ユーザーマネージャー:
public CustomUserManager(IUserStore<User> store,
IPasswordHasher passwordHasher,
IIdentityMessageService emailService)
: base(store)
{
//Other validators
PasswordValidator = new StrongPasswordValidator
{
MinimumLength = 8,
MaximumLength = 100
};
PasswordHasher = passwordHasher;
EmailService = emailService;
}
ユーザーの作成:
var userManager = new CustomUserManager(userStore,
new BCryptHasher(), emailService.Object);
userManager.CreateAsync(user, Password);