<bdo id='lejE0'></bdo><ul id='lejE0'></ul>

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

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

        ASP.NET Core 标识:角色管理器没有服务

        ASP.NET Core Identity: No service for role manager(ASP.NET Core 标识:角色管理器没有服务)
      1. <legend id='nockp'><style id='nockp'><dir id='nockp'><q id='nockp'></q></dir></style></legend>

                <tbody id='nockp'></tbody>
              <tfoot id='nockp'></tfoot>

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

                  <bdo id='nockp'></bdo><ul id='nockp'></ul>

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

                • 本文介绍了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() 时,您尝试解决 RoleManagerIdentityRole,相当于 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 标识:角色管理器没有服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  asp.net mvc Adding to the AUTHORIZE attribute(asp.net mvc 添加到 AUTHORIZE 属性)
                  Crystal Reports: Passing Multiline Parameter Values(Crystal Reports:传递多行参数值)
                  Bar code font error with crystal report on web server(Web 服务器上水晶报告的条形码字体错误)
                  space between Crystal Reports Cross-Tab Column(Crystal Reports 交叉表列之间的空间)
                  Pass instance of Class as parameter to Attribute constructor(将 Class 的实例作为参数传递给 Attribute 构造函数)

                    <legend id='2Z3H6'><style id='2Z3H6'><dir id='2Z3H6'><q id='2Z3H6'></q></dir></style></legend>

                    <small id='2Z3H6'></small><noframes id='2Z3H6'>

                    • <tfoot id='2Z3H6'></tfoot>

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