问题描述
在 ASP.NET Core 应用程序中,我想创建某些角色作为管理不同用户权限的基础.可悲的是,文档详细说明了如何使用自定义角色,例如在控制器/动作中,但不是如何创建它们.我发现我可以为此使用 RoleManager<IdentityRole>
,当在应用程序中注册其定义的和 ASP.NET Core 身份时,实例会自动注入到控制器构造函数中.
In an ASP.NET Core application, I want to create certain roles as a basis to manage different user-permissions. Sadly, the documentation inform detailled how to use custom roles e.g. in controllers/actions, but not how to create them. I found out that I can use RoleManager<IdentityRole>
for this, where the instance gets automatically injected in a controller-constructor, when its defined and ASP.NET Core identity is registered in the application.
这让我可以像这样添加一个自定义角色:
This let me add a custom role like this:
var testRole = new IdentityRole("TestRole");
if(!roleManager.RoleExistsAsync(testRole.Name).Result) {
roleManager.CreateAsync(testRole);
}
它工作并在数据库中创建角色.但是这个检查总是会在数据库上产生开销,调用特定的控制器/动作.所以我想在我的应用程序启动后检查一次,如果自定义角色存在并添加它们.Startup.cs 中的 ConfigureServices
方法似乎对此很有用.
It works and create the role in the database. But this check will always create overhead on the database, calling the specific controller/action. So I want to check once after my application has started, if the custom role exists and add them. The ConfigureServices
method in Startup.cs seems good for this.
但是:如何创建 RoleManager
类的实例来执行此操作?我想在这里使用最佳实践方法,而不是通过自己创建依赖实例来搞乱,这似乎会导致很多工作,因为它没有很好的文档记录并且肯定不会遵循最佳实践,因为 ASP.NET Core 正在使用像这样的事情的依赖注入(这在我看来也是合理的).
But: How can I create a instance of the RoleManager<IdentityRole>
class for doing this? I would like to use a best practice approach here and not messing around by creating depending instances on my own, which seems to cause a lot of work since its not good documentated and will surely not follow best practices, since ASP.NET Core is using dependency injection for things like this (which is also reasonable in my oppinion).
换句话说:我需要在控制器的外部使用依赖注入.
In other words: I need to use dependeny injection outside of a controller.
推荐答案
下面是一个示例,满足您在启动时迁移和植入数据库的需求:
Here is an example for your needs that migrates and seeds the database on startup:
创建一个静态类:
public static class RolesData
{
private static readonly string[] Roles = new string[] {"Administrator", "Editor", "Subscriber"};
public static async Task SeedRoles(IServiceProvider serviceProvider)
{
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
if (dbContext.Database.GetPendingMigrations().Any())
{
await dbContext.Database.MigrateAsync();
var roleManager = serviceScope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
foreach (var role in Roles)
{
if (!await roleManager.RoleExistsAsync(role))
{
await roleManager.CreateAsync(new IdentityRole(role));
}
}
}
}
}
}
在Startup.cs
中:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
RolesData.SeedRoles(app.ApplicationServices).Wait();
}
这篇关于ASP.NET Core Identity 在应用程序启动时添加自定义用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!