ASP.NET Core身份:角色管理器无服务 [英] ASP.NET Core Identity: No service for role manager

查看:135
本文介绍了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中,我已将Identity和角色管理器添加为如下范围服务:

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>>();

和在启动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:创建项目以从头开始学习Identity时,我使用了无身份验证".

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<int>调用services.AddIdentity()时,尝试使用IdentityRole解决RoleManager,这等效于IdentityRole<string>(string是ASP.NET Core Identity中的默认密钥类型).

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身份:角色管理器无服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆