Fab*_*ves 5 c# asp.net-core asp.net-core-identity
我需要为ROLES创建CRUD操作。
我收到以下错误:
“无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager`的服务”
那么,我该如何注入roleManager?
我正在使用ASP Net Core 2.0 + Identity 2.2.1
类ApplicationUser
public class ApplicationUser : IdentityUser
{
[Key]
public override string Id { get; set; }
public bool Type { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在在Startup.cs中
services.AddIdentity<ApplicationUser, IdentityRole<int>>()
.AddUserStore<UserStore<ApplicationUser, IdentityRole<int>, ApplicationDbContext, int>>()
.AddRoleStore<RoleStore<IdentityRole<int>, ApplicationDbContext, int>>()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
控制者
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityUser> _roleManager;
public RolesController(UserManager<ApplicationUser> userManager, RoleManager<IdentityUser> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
}
public IActionResult Index()
{
return View(_roleManager.Roles);
}
Run Code Online (Sandbox Code Playgroud)
因此,我收到错误消息:“无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager`的服务。
小智 3
您应该使用 .AddRoleManager<> 而不是 AddRoleStore<>。或者两者都适用,以防您想创建新角色。如果您不使用自定义商店,请尝试使用:AddEntityFrameworkStores
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<YourContext>()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2359 次 |
| 最近记录: |