ASP.NET Core Identity: No service for role manager(ASP.NET Core 标识:角色管理器没有服务)
问题描述
我有一个使用 Identity 的 ASP.NET Core 应用程序.它可以工作,但是当我尝试向数据库添加自定义角色时,我遇到了问题.
I have an ASP.NET Core app that uses Identity. It works, but when I am trying to add custom roles to the database I run into problems.
在启动 ConfigureServices 我已将身份和角色管理器添加为这样的范围服务:
In Startup ConfigureServices I have added Identity and the role manager as a scoped service like this:
services.AddIdentity<Entities.DB.User, IdentityRole<int>>()
.AddEntityFrameworkStores<MyDBContext, int>();
services.AddScoped<RoleManager<IdentityRole>>();
在 Startup Configure 我注入 RoleManager 并将其传递给我的自定义类 RolesData:
and in Startup Configure I inject RoleManager and pass it to my custom class RolesData:
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
RoleManager<IdentityRole> roleManager
)
{
app.UseIdentity();
RolesData.SeedRoles(roleManager).Wait();
app.UseMvc();
这是 RolesData 类:
public static class RolesData
{
private static readonly string[] roles = new[] {
"role1",
"role2",
"role3"
};
public static async Task SeedRoles(RoleManager<IdentityRole> roleManager)
{
foreach (var role in roles)
{
if (!await roleManager.RoleExistsAsync(role))
{
var create = await roleManager.CreateAsync(new IdentityRole(role));
if (!create.Succeeded)
{
throw new Exception("Failed to create role");
}
}
}
}
}
应用程序构建时没有错误,但在尝试访问它时出现以下错误:
The app builds without errors, but when trying to access it I get the following error:
无法解析类型 'Microsoft.AspNetCore.Identity.IRoleStore`1[Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole]' 的服务尝试激活 'Microsoft.AspNetCore.Identity.RoleManager
Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IRoleStore`1[Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager
我做错了什么?我的直觉说我将 RoleManager 添加为服务的方式有问题.
What am I doing wrong? My gut says there's something wrong with how I add the RoleManager as a service.
PS:我在创建项目时使用无身份验证"从头开始学习身份.
PS: I have used "No authentication" when creating the project to learn Identity from scratch.
推荐答案
我做错了什么?我的直觉说我将 RoleManager 添加为服务的方式有问题.
What am I doing wrong? My gut says there's something wrong with how I add the RoleManager as a service.
注册部分实际上没问题,但您应该删除 services.AddScoped<RoleManager<IdentityRole>>(),因为 services 已经为您添加了角色管理器.AddIdentity().
The registration part is actually fine, tho' you should remove services.AddScoped<RoleManager<IdentityRole>>(), as the role manager is already added for you by services.AddIdentity().
您的问题很可能是由泛型类型不匹配引起的:当您使用 IdentityRole 调用 services.AddIdentity() 时,您尝试解决 RoleManager 和 IdentityRole,相当于 IdentityRole(string 是 ASP.NET Core 中的默认键类型身份).
Your issue is most likely caused by a generic type mismatch: while you call services.AddIdentity() with IdentityRole<int>, you try to resolve RoleManager with IdentityRole, which is an equivalent of IdentityRole<string> (string being the default key type in ASP.NET Core Identity).
更新您的 Configure 方法以采用 RoleManager<IdentityRole<int>> 参数,它应该可以工作.
Update your Configure method to take a RoleManager<IdentityRole<int>> parameter and it should work.
这篇关于ASP.NET Core 标识:角色管理器没有服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.NET Core 标识:角色管理器没有服务
基础教程推荐
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 如果条件可以为空 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
