【发布时间】:2017-02-11 01:05:41
【问题描述】:
默认情况下,ASP.NET Core Identity 的密码策略要求至少有一个特殊字符、一个大写字母、一个数字……
如何更改此限制?
文档中对此一无所知 (https://docs.asp.net/en/latest/security/authentication/identity.html)
我尝试覆盖身份的用户管理器,但我没有看到管理密码策略的方法。
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(
DbContextOptions<SecurityDbContext> options,
IServiceProvider services,
IHttpContextAccessor contextAccessor,
ILogger<UserManager<ApplicationUser>> logger)
: base(
new UserStore<ApplicationUser>(new SecurityDbContext(contextAccessor)),
new CustomOptions(),
new PasswordHasher<ApplicationUser>(),
new UserValidator<ApplicationUser>[] { new UserValidator<ApplicationUser>() },
new PasswordValidator[] { new PasswordValidator() },
new UpperInvariantLookupNormalizer(),
new IdentityErrorDescriber(),
services,
logger
// , contextAccessor
)
{
}
public class PasswordValidator : IPasswordValidator<ApplicationUser>
{
public Task<IdentityResult> ValidateAsync(UserManager<ApplicationUser> manager, ApplicationUser user, string password)
{
return Task.Run(() =>
{
if (password.Length >= 4) return IdentityResult.Success;
else { return IdentityResult.Failed(new IdentityError { Code = "SHORTPASSWORD", Description = "Password too short" }); }
});
}
}
public class CustomOptions : IOptions<IdentityOptions>
{
public IdentityOptions Value { get; private set; }
public CustomOptions()
{
Value = new IdentityOptions
{
ClaimsIdentity = new ClaimsIdentityOptions(),
Cookies = new IdentityCookieOptions(),
Lockout = new LockoutOptions(),
Password = null,
User = new UserOptions(),
SignIn = new SignInOptions(),
Tokens = new TokenOptions()
};
}
}
}
我在启动类中添加了这个用户管理器依赖:
services.AddScoped<ApplicationUserManager>();
但是当我在控制器中使用 ApplicationUserManager 时,出现错误: 处理请求时发生未处理的异常。
InvalidOperationException:尝试激活“ApplicationUserManager”时,无法解析“Microsoft.EntityFrameworkCore.DbContextOptions`1[SecurityDbContext]”类型的服务。
编辑:当我使用 ASP.NET Core Identity 的默认类时,用户的管理工作正常,所以这不是数据库问题,或类似的问题
编辑 2:我找到了解决方案,您只需在启动类中配置 Identity。我的回答提供了一些细节。
【问题讨论】:
-
有趣的事实:MS 强加的默认设置对于 chrome 密码管理器生成的密码来说太严格了。
标签: c# asp.net-core-mvc asp.net-identity asp.net-core-identity