在以前的项目中,我在App_start文件夹的identityconfig.cs
文件中定义了.NET身份的密码验证。以下内容将在ApplicationUserManager类下:
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
在.NET MVC的最新版本中,我使用identityconfig.cs
并删除了App_Start。
我一直无法找到关于现在可以设置这些变量的明确答案。希望有人能指出我正确的方向。
答案 0 :(得分:2)
如果您在谈论ASP.NET Core MVC 6中的Identity 3
您需要通过实现接口IPasswordValidator<T>
来实现对象中的自定义逻辑。
然后在IServiceProvider ConfigureServices(IServiceCollection services)
方法下的Startup.cs中
添加以下
services.AddScoped<IPasswordValidator<ApplicationUser>, YOUR_OBJECT>();
其中ApplicationUser是Identity实现使用的用户对象
Identity 3已被拆分为可扩展性接口
答案 1 :(得分:1)
这是配置ApplicationUserManager
课程的一部分。它只是一个类,所以 无关紧要。
但是,由于我将所有实体保存在一个单独的类库中(包括我的ApplicationUser
实体),我喜欢在那里创建一个Identity
目录,ApplicationUser
,{{ 1}},ApplicationUserManager
等,每个都在自己的.cs文件中以类命名,这几乎是任何类的最佳实践。微软喜欢把所有内容都放在几个.cs文件中,但这在技术上是不好的做法。
但这就是我这样做的方式。您可以自由组织解决方案,但是您觉得对您的应用程序最有意义。
答案 2 :(得分:1)
我通过在startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add Entity Framework services to the services container.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>(o => {
// configure identity options
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonLetterOrDigit = false; ;
o.Password.RequiredLength = 6;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();