尝试激活 'Management.Controllers.RoleController' 时无法解析类型 'Microsoft.AspNetCore.Identity.UserManager' 的服务

Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'Management.Controllers.RoleController'

我想创建角色并将用户分配给这些角色,我已经创建了角色但我不知道如何将用户分配给他们,我尝试在启动时将 UserManager 添加到服务但它不起作用。我有这个错误:

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[PFE_Management.Models.AppUser]' while attempting to activate 'PFE_Management.Controllers.RoleController'.

这个控制器:

namespace PFE_Management.Controllers
{
    public class RoleController : Controller
    {
        private RoleManager<IdentityRole> roleManager;
        private UserManager<AppUser> userManager;
        public RoleController(RoleManager<IdentityRole> roleMgr, UserManager<AppUser> userMrg)
        {
            roleManager = roleMgr;
            userManager = userMrg;
        }

        //some code here
        // le code qui suive c'est ajouter ou supprimer un utilisateur pour un Role
        public async Task<IActionResult> Update(string id)
        {
            //some code here
        }
        [HttpPost]
        public async Task<IActionResult> Update(RoleModification model)
        {
            IdentityResult result;
            if (ModelState.IsValid)
            {
                foreach (string userId in model.AddIds ?? new string[] { })
                {
                    AppUser user = await userManager.FindByIdAsync(userId);
                    if (user != null)
                    {
                        result = await userManager.AddToRoleAsync(user, model.RoleName);
                        if (!result.Succeeded)
                            Errors(result);
                    }
                }
                foreach (string userId in model.DeleteIds ?? new string[] { })
                {
                    AppUser user = await userManager.FindByIdAsync(userId);
                    if (user != null)
                    {
                        result = await userManager.RemoveFromRoleAsync(user, model.RoleName);
                        if (!result.Succeeded)
                            Errors(result);
                    }
                }
            }

            if (ModelState.IsValid)
                return RedirectToAction(nameof(Index));
            else
                return await Update(model.RoleId);
        }
    }
}

这是 startup.cs 中的服务:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)                
            .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddControllersWithViews();
        services.AddRazorPages();    
    }

AddDefaultIdentity 不会将用户管理器添加到服务集合中。你需要自己添加。例如:

services.AddDefaultIdentity<IdentityUser>(...)                
    .AddRoles<IdentityRole>()
    .AddUserManager<IdentityUser>() // <-- add this
    .AddEntityFrameworkStores<ApplicationDbContext>();