c# - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'WebShop.Controllers.User.UserController'
问题描述
Im new at Entity-framework. And im working on a webapplication, I want to make a userauthentication modul to an Angular site. User login work perfectly.When i try to add user roles i got the following error message:
And I got the following error: error
I using JWT token for authenticator. And Microsoft.IdentityModel.
My code: Startup.cs Identity service:
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<WebShopContext>();
JWT aut service:
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
};
});
}
My ApplicationRole model:
public class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string name) : base(name) { }
public string Description { get; set; }
}
}
My ApplicationUser model:
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; }
}
I have absolutly no idea. How can I solve this problem. Please help me.
解决方案
尝试改变
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<WebShopContext>();
至
services.AddIdentity<ApplicationUser, IdentityRole>() // <--- changed to ApplicationUser
.AddEntityFrameworkStores<WebShopContext>();
我将假设,通过阅读错误,您的模型继承自IdentityUser
isApplicationUser
并且您正在设置您的服务来寻找IdentityUser
而不是ApplicationUser
您所要求的。
推荐阅读
- spring-boot - Spring Boot:本地化 REST API 响应
- batch-file - 如何在vbs中将字符串添加到传递的参数中
- javascript - 我的 js 代码有什么问题。结果总是不确定的?
- javascript - JavaScript 函数在 AJAX XML HttpRequest 之后不起作用
- sql - Role of null in performance tuning
- javascript - Xpath 返回下元素的空值
标签 - python - PermissionError 仅在第一次引发 - pd.ExcelWriter.save()
- c++ - 如何匹配环境不同的两组?C++
- java - 减少 Hibernate 存储库中的样板代码/在桌面客户端-服务器应用程序中使用 Spring 的 JpaRepository 接口
- ios - 在真实设备上获取位置不起作用