[英]Custom UserManager Asp.net MVC
Im trying to 'extend' the UserManager in an ASP.net MVC web app.我试图在 ASP.net MVC web 应用程序中“扩展”UserManager。 I have read through a couple of previous posts regarding this, and I don't seem to be getting in correct.我已经阅读了之前关于此的几篇文章,但我似乎没有理解正确。
Extending the UserManager 扩展用户管理器
I have made a MyManager class that inherits UserManger:我做了一个继承了UserManger的MyManager class:
namespace DHS_Intranet.Helpers
{
public class MyManager : UserManager<ApplicationUser>
{
public MyManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators, IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger)
: base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
{
}
public async Task<bool> IsFirstLoginAsync(ApplicationUser user)
{
return (bool)user.FirstLogin;
}
public async Task<bool> IsPasswordChangeRequired(ApplicationUser user)
{
return user.ForcePasswordChange ?? true;
}
}
}
I have registered this in the Program.cs
我已经在Program.cs
中注册了这个
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequireNonAlphanumeric = false;
options.User.RequireUniqueEmail = true;
options.SignIn.RequireConfirmedAccount = false;
options.SignIn.RequireConfirmedEmail = false;
})
.AddRoles<IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddUserManager<MyManager>();
However when I try and register this in my controller public AccountController(MyManager<ApplicationUser> userManager, ...
但是,当我尝试在我的 controller public AccountController(MyManager<ApplicationUser> userManager, ...
I get the error: Error CS0308: The non-generic type 'MyManager' cannot be used with type arguments (CS0308)
我收到错误: Error CS0308: The non-generic type 'MyManager' cannot be used with type arguments (CS0308)
I'm not sure what I am doing wrong.我不确定我做错了什么。
Any pointers would be helpful.任何指针都会有所帮助。
Thanks谢谢
James詹姆士
I realised what I was doing wrong;我意识到我做错了什么; my custom UserManager class should look like this:我的自定义 UserManager class 应该如下所示:
namespace DHS_Intranet.Helpers
{
public class MyManager<ApplicationUser> : UserManager<ApplicationUser>
{
public MyManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators, IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger)
: base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
{
}
public async Task<bool> IsFirstLoginAsync(ApplicationUser user)
{
return (bool)user.FirstLogin;
}
public async Task<bool> IsPasswordChangeRequired(ApplicationUser user)
{
return user.ForcePasswordChange ?? true;
}
}
}
I didn't supply type to the MyManager我没有向 MyManager 提供类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.