我是 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/