没有注册类型'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

查看:599
本文介绍了没有注册类型'Microsoft.AspNetCore.Identity.UserManager`1 [Microsoft.AspNetCore.Identity.IdentityUser]的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此错误的可能原因是什么?

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; }
}

我的DbContext:

My DbContext:

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);
}

我想了解为什么会发生这种情况以及最佳做法是什么。 / p>

i want to understand why this is happening and what is the best practice.

推荐答案

注册自己的 MyUserStore 时(错误名称,应为MyUser)对于AspNetCore身份,UserManager<>类型将作为 UserManager< MyUserStore> 注册到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<> 时,请将在启动时注册的身份用户模型指定为type参数。应为 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.

调用 GetRequiredService< UserManager< IdentityUser>>时;()在结果服务提供者上, GetRequiredService< UserManager< IdentityUser>>()会抛出上述异常。

When calling GetRequiredService<UserManager<IdentityUser>>() on the resulting service provider, GetRequiredService<UserManager<IdentityUser>>() will throw the above exception.

这通常发生在剃刀视图中。例如,

This usually happens in razor views. Eg.

@inject Microsoft.AspNetCore.Identity.UserManager<Microsoft.AspNetCore.Identity.IdentityUser> userManager

或者类似地,在其他类中自己调用时,可能就是这样播种机服务。或其他代码部分。异常的调用堆栈应该给您提示发生情况的地方。

Or like-wise, when calling it yourself inside other classes, as may be the case in your Seeder service. Or other code portions. 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆