<small id='5lFul'></small><noframes id='5lFul'>

  • <legend id='5lFul'><style id='5lFul'><dir id='5lFul'><q id='5lFul'></q></dir></style></legend>
        <bdo id='5lFul'></bdo><ul id='5lFul'></ul>
      <tfoot id='5lFul'></tfoot>

      1. <i id='5lFul'><tr id='5lFul'><dt id='5lFul'><q id='5lFul'><span id='5lFul'><b id='5lFul'><form id='5lFul'><ins id='5lFul'></ins><ul id='5lFul'></ul><sub id='5lFul'></sub></form><legend id='5lFul'></legend><bdo id='5lFul'><pre id='5lFul'><center id='5lFul'></center></pre></bdo></b><th id='5lFul'></th></span></q></dt></tr></i><div id='5lFul'><tfoot id='5lFul'></tfoot><dl id='5lFul'><fieldset id='5lFul'></fieldset></dl></div>

      2. ASP.NET Core Identity 在应用程序启动时添加自定义用户角色

        时间:2023-07-11
          <bdo id='77cy1'></bdo><ul id='77cy1'></ul>

          <small id='77cy1'></small><noframes id='77cy1'>

          <legend id='77cy1'><style id='77cy1'><dir id='77cy1'><q id='77cy1'></q></dir></style></legend><tfoot id='77cy1'></tfoot>

            <i id='77cy1'><tr id='77cy1'><dt id='77cy1'><q id='77cy1'><span id='77cy1'><b id='77cy1'><form id='77cy1'><ins id='77cy1'></ins><ul id='77cy1'></ul><sub id='77cy1'></sub></form><legend id='77cy1'></legend><bdo id='77cy1'><pre id='77cy1'><center id='77cy1'></center></pre></bdo></b><th id='77cy1'></th></span></q></dt></tr></i><div id='77cy1'><tfoot id='77cy1'></tfoot><dl id='77cy1'><fieldset id='77cy1'></fieldset></dl></div>
              <tbody id='77cy1'></tbody>

                  本文介绍了ASP.NET Core Identity 在应用程序启动时添加自定义用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 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 在应用程序启动时添加自定义用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ASP.Net MVC 5 中的 Cookie 下一篇:带有 Windows 身份验证的 ASP.NET Core 2.1 自定义 RoleProvider

                  相关文章

                  <legend id='kHruT'><style id='kHruT'><dir id='kHruT'><q id='kHruT'></q></dir></style></legend>

                    <small id='kHruT'></small><noframes id='kHruT'>

                    1. <i id='kHruT'><tr id='kHruT'><dt id='kHruT'><q id='kHruT'><span id='kHruT'><b id='kHruT'><form id='kHruT'><ins id='kHruT'></ins><ul id='kHruT'></ul><sub id='kHruT'></sub></form><legend id='kHruT'></legend><bdo id='kHruT'><pre id='kHruT'><center id='kHruT'></center></pre></bdo></b><th id='kHruT'></th></span></q></dt></tr></i><div id='kHruT'><tfoot id='kHruT'></tfoot><dl id='kHruT'><fieldset id='kHruT'></fieldset></dl></div>

                      • <bdo id='kHruT'></bdo><ul id='kHruT'></ul>
                      <tfoot id='kHruT'></tfoot>