没有注册“Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]"类型的服务 [英] No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered
问题描述
此错误的可能原因是什么:
What is the possible cause of this error:
InvalidOperationException: 没有注册Microsoft.AspNetCore.Identity.UserManager [Microsoft.AspNetCore.Identity.IdentityUser]"类型的服务.
InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager [Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.
我的目标框架是netcoreapp2.1
.
这是我的用户存储类:
public class MyUserStore : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
还有我的用户角色类:
public class MyUserRole : IdentityRole
{
public string Description { get; set; }
}
我的数据库上下文:
public class ApplicationDbContext : IdentityDbContext<MyUserStore,MyUserRole,string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext>
options): base(options) { }
}
我在Startup.cs
中的ConfigureServices
方法:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//services.AddDefaultIdentity<IdentityUser>()
// .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<MyUserStore, MyUserRole>(cfg => {
cfg.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<ApplicationDbContext>();
services.AddTransient<Seeder>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
我想了解为什么会发生这种情况以及最佳做法是什么.
i want to understand why this is happening and what is the best practice.
推荐答案
这通常发生在 _LoginPartial.cshtml razor 视图中.例如.
This usually happens in the _LoginPartial.cshtml razor view. Eg.
@inject UserManager<IdentityUser> userManager
必须改为
@inject UserManager<MyUserStore> userManager
同样适用于 SignInManager
.
当为 AspNetCore Identity 注册您自己的 MyUserStore
(坏名,应该是 MyUser)时,UserManager<>类型将作为 UserManager
注册到 ServiceCollection.
When registering your own MyUserStore
(bad name, should be MyUser) for the AspNetCore Identity, the UserManager<> type will be registered to the ServiceCollection as UserManager<MyUserStore>
.
每当您想要解析 UserManager<>
时,请将在您的启动中注册的身份用户模型指定为类型参数. 这将是 UserManager<>MyUserStore>
在您的特定情况下:
Whenever you want to resolve the UserManager<>
, specify the identity user model registered in your startup as the type parameter. Which would be UserManager<MyUserStore>
in your specific case:
或者类似地,在其他类中解析它时,例如在您的 Seeder
服务中.您的异常的调用堆栈应该会提示您发生这种情况.
Or like-wise, when resolving it inside other classes, as may be the case in your Seeder
service. The call stack of your exception should give you a hint of where this is happening.
这篇关于没有注册“Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]"类型的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!