从我已经从For RoleManager <trole>派生的东西派生的异常

时间:2015-12-15 18:26:34

标签: asp.net-identity asp.net-core

问题信息

用例:我试图在我的构造函数中使用ApplicationRoleManager作为asp net 5控制器。我的ApplicationRoleManager基本上是具体实现的公共类RoleManager:IDisposable其中TRole:class 有人可以给我看一些示例代码,如何使用// .AddRoleManager()而不是我的解决方法。

技术:Asp Net 5 例外:异常:附加信息:键入ApplicationRoleManager必须 派生自RoleManager。

在我的Startup.cs

  services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddUserManager<CustomUserManager>()
                //EXCEPTION OCCURS only when I try to AddRolemanager
                .AddRoleManager<ApplicationRoleManager>()
                .AddRoleStore<ApplicationRoleStore>()
                .AddDefaultTokenProviders();

我的模特

 public class ApplicationRole : IdentityRole<string>
    {
        public ApplicationRole()
        {
            this.Id = Guid.NewGuid().ToString();
        }
        public ApplicationRole(string name)
           : this()
        {
            this.Name = name;
        }
    }

可以预见,我尝试使用RoleManager,其中Application角色来自身份

public class ApplicationRoleManager : RoleManager<ApplicationRole>
    {
        public ApplicationRoleManager(
            IRoleStore<ApplicationRole>store, 
            IEnumerable<IRoleValidator<ApplicationRole>> roleValidators,
            ILookupNormalizer keyNormalizer,
            IdentityErrorDescriber errors,
            ILogger<RoleManager<ApplicationRole>>  logger,
            IHttpContextAccessor contextAccessor
            )
           :base(
                    store:store,
                    roleValidators: roleValidators, 
                    keyNormalizer: keyNormalizer,
                    errors:errors,
                    contextAccessor: contextAccessor,
                    logger: logger)
        {
        }

    }

我的工作

我设法让我的控制器中的依赖项工作如下所示,但我的目标是学习如何使用.AddRoleManager

   public GroupsAdminController(ApplicationRoleManager roleManager)

{      //所以现在如果我添加services.AddScoped等...在启动时它不会抛出错误。  }

 services.AddScoped<ApplicationRoleManager, ApplicationRoleManager>();
services.AddScoped<IRoleStore<ApplicationRole>, ApplicationRoleStore>();
 services.AddScoped<DbContext, ApplicationDbContext>();

1 个答案:

答案 0 :(得分:2)

我改变了  services.AddIdentity<ApplicationUser, ApplicationRole>()

this(...)

注意到有一个类型mismacth ApplicationRoleManager!