c# - 尝试激活 'Microsoft.AspNetCore.Identity.UserManager' 时无法解析类型 'WebShop.Controllers.User.UserController' 的服务

标签 c# entity-framework asp.net-core .net-core entity-framework-core

我是 Entity-framework 的新手。 我正在开发一个网络应用程序,我想为 Angular 站点创建一个用户身份验证模块。 用户登录工作完美。当我尝试添加用户角色时,我收到以下错误消息:

我得到了以下错误: error

我使用 JWT token 作为身份验证器。 和 Microsoft.IdentityModel。

我的代码: 启动.cs 身份服务:

            services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<WebShopContext>();

JWT 自动服务:

 var key = Encoding.UTF8.GetBytes(Configuration["ApplicationSettings:JWT_Secret"]);

        services.AddAuthentication(x => 
        {
            x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(x=> {
            x.RequireHttpsMetadata = false;
            x.SaveToken = false;
            x.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(key),
                ValidateIssuer = false,
                ValidateAudience = false,
                ClockSkew = TimeSpan.Zero 
            };
        });
    }

我的 ApplicationRole 模型:

public class ApplicationRole : IdentityRole
    {
        public ApplicationRole() : base() { }
        public ApplicationRole(string name) : base(name) { }
        public string Description { get; set; }
    }
}

我的 ApplicationUser 模型:

    public class ApplicationUser : IdentityUser
{
    [Column(TypeName="nvarchar(150)")]
    public string FullName { get; set; }

    [Column(TypeName = "nvarchar(100)")]
    public string FirstName { get; set; }

    [Column(TypeName = "nvarchar(100)")]
    public string LastName { get; set; }

}

我完全不知道。我怎么解决这个问题。请帮助我。

最佳答案

尝试改变

            services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<WebShopContext>();

      services.AddIdentity<ApplicationUser, IdentityRole>() // <--- changed to ApplicationUser
      .AddEntityFrameworkStores<WebShopContext>();

我将假设,通过读取错误,您从 IdentityUser 继承的模型是 ApplicationUser 并且您正在设置您的服务以寻找 IdentityUser 而不是 ApplicationUser 这正是您所要求的。

关于c# - 尝试激活 'Microsoft.AspNetCore.Identity.UserManager' 时无法解析类型 'WebShop.Controllers.User.UserController' 的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58883453/

相关文章:

c# - 我应该如何为 dnx451 和 dnxcore50 引用 HttpClient?

c# - Windows CE 控件的设计是否没有自动化支持?

C# - 如何在 Entity Framework Core 3.1 的模型中动态设置 builder.Property().HasComment

c# - 使用 EF 核心时使用 IN 子句的原始查询

c# - 如何在 cshtml View 中使用 Asp .NET Core MVC 中的变量?

c# - 按 id 选择子集的所有项目

c# - 使用属性在事件上使用目标

c# - Entity Framework 代码首先初始化外键

c# - .NET核心6 : Use my DbContext in Authorization Handler

c# - 在 ASP.NET Core 中多次调用 app.Run() 有什么意义?