[英]ASP.NET Core Identity: No service for role manager
我有一個使用Identity的ASP.NET Core應用程序。 它可以工作,但是當我嘗試將自定義角色添加到數據庫時,我遇到了問題。
在Startup ConfigureServices
我添加了Identity和角色管理器作為范圍服務,如下所示:
services.AddIdentity<Entities.DB.User, IdentityRole<int>>()
.AddEntityFrameworkStores<MyDBContext, int>();
services.AddScoped<RoleManager<IdentityRole>>();
在啟動Configure
我注入RoleManager並將其傳遞給我的自定義類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");
}
}
}
}
}
應用程序構建沒有錯誤,但在嘗試訪問它時,我收到以下錯誤:
嘗試激活'Microsoft.AspNetCore.Identity.RoleManager時無法解析類型'Microsoft.AspNetCore.Identity.IRoleStore`1 [Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole]'的服務
我究竟做錯了什么? 我的直覺說我將RoleManager添加為服務有什么問題。
PS:在創建項目以從頭學習身份時,我使用了“無身份驗證”。
我究竟做錯了什么? 我的直覺說我將RoleManager添加為服務有什么問題。
注冊部分實際上很好,你應該刪除services.AddScoped<RoleManager<IdentityRole>>()
,因為services.AddIdentity()
已經為你添加了角色管理器。
您的問題很可能是由泛型類型不匹配引起的:當您使用IdentityRole<int>
調用services.AddIdentity()
時,您嘗試使用IdentityRole
解析RoleManager
, IdentityRole
等效於IdentityRole<string>
( string
是默認鍵類型在ASP.NET核心身份中)。
更新您的Configure
方法以獲取RoleManager<IdentityRole<int>>
參數,它應該可以工作。
我遇到了這個問題
沒有類型'Microsoft.AspNetCore.Identity.RoleManager`的服務
此頁面是Google上的第一個結果。 它沒有回答我的問題,所以我想我會把我的解決方案放在這里,對於任何可能遇到這個問題的人。
ASP.NET Core 2.2
我缺少的是Startup.cs文件中的.AddRoles() 。
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>();
希望這有助於某人
來源: https : //docs.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-2.2 (在底部)
這是我的解決方案種子用戶和角色ASP.NET Core 2.2
Startup.cs
services.AddDefaultIdentity<ApplicationUser>()
.AddRoles<IdentityRole<Guid>>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>();
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
...
...
SeedData.Initialize(app.ApplicationServices);
)
SeedData.cs
public static void Initialize(IServiceProvider serviceProvider)
{
using (var scope = serviceProvider.CreateScope())
{
var provider = scope.ServiceProvider;
var context = provider.GetRequiredService<ApplicationDbContext>();
var userManager = provider.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = provider.GetRequiredService<RoleManager<IdentityRole<Guid>>>();
// automigration
context.Database.Migrate();
InstallUsers(userManager, roleManager);
}
}
private static void InstallUsers(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole<Guid>> roleManager)
{
const string USERNAME = "admin@mysite.com";
const string PASSWORD = "123456ABCD";
const string ROLENAME = "Admin";
var roleExist = roleManager.RoleExistsAsync(ROLENAME).Result;
if (!roleExist)
{
//create the roles and seed them to the database
roleManager.CreateAsync(new IdentityRole<Guid>(ROLENAME)).GetAwaiter().GetResult();
}
var user = userManager.FindByNameAsync(USERNAME).Result;
if (user == null)
{
var serviceUser = new ApplicationUser
{
UserName = USERNAME,
Email = USERNAME
};
var createPowerUser = userManager.CreateAsync(serviceUser, PASSWORD).Result;
if (createPowerUser.Succeeded)
{
var confirmationToken = userManager.GenerateEmailConfirmationTokenAsync(serviceUser).Result;
var result = userManager.ConfirmEmailAsync(serviceUser, confirmationToken).Result;
//here we tie the new user to the role
userManager.AddToRoleAsync(serviceUser, ROLENAME).GetAwaiter().GetResult();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.