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

ASP.NET Core Identity: No service for role manager(ASP.NET Core 标识:角色管理器没有服务)
本文介绍了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 标识:角色管理器没有服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)