![](/img/trans.png)
[英]How to configure EmailService class in IdentityConfig.cs to send email for password reset in asp.net
[英]Where is the correct place to define password validation in ASP.NET 5.0 MVC Identity now identityconfig.cs has been removed?
在以前的项目中,我已经在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已被删除。
我一直无法找到关于现在可以在哪里设置这些变量的明确答案。 希望有人可以指出正确的方向。
如果您正在谈论ASP.NET Core MVC 6中的Identity 3,则需要通过实现IPasswordValidator<T>
接口在对象中实现自定义逻辑。
然后在您的Startup.cs中的IServiceProvider ConfigureServices(IServiceCollection services)
方法下,添加以下内容
services.AddScoped<IPasswordValidator<ApplicationUser>, YOUR_OBJECT>();
其中ApplicationUser是您的Identity实现使用的用户对象
身份3已拆分为可扩展性的接口
这是配置ApplicationUserManager
类的一部分。 这只是一堂课,所以去哪里都没有关系。
但是,由于我将所有实体保存在单独的类库中(包括我的ApplicationUser
实体),因此我希望在该目录中创建一个Identity
目录,在其中将ApplicationUser
, ApplicationUserManager
和ApplicationSignInManager
等放置在各自的.cs文件中,该文件以。类,这几乎是所有类的最佳实践。 Microsoft喜欢将所有内容都放入几个.cs文件中,但这在技术上是不正确的做法。
不过,那只是我做的方式。 您可以自由组织解决方案,但是您觉得对您的应用程序最有意义。
我是通过在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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.