无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager的服务

时间:2020-07-21 00:52:02

标签: asp.net-core

我希望你们一切都好!我收到以下错误:

export default class SubmitModal {
  constructor () {
    this.element = document.createElement('div')
    this.element.classList.add('modal')
    this.element.appendChild(this.generateContent());
  }

  generateContentHTML () {
    let div = document.createElement("div");
    div.classList.add("modal-content");
    div.innerHTML = `a bunch of other things`;
    let button = document.createElement("button");
    button.innerText = 'Submit';
    button.addEventListener("click", () => this.submit());
    div.appendChild(button);
    return div;
  }

  submit () {
     console.log('submitted!')
  }
}

我花了数周的时间来尝试解决该错误,在这里阅读论坛和其他网站。网上的评论建议修改我的Startup.cs文件,但是我尝试了许多不同的方法,但是我仍然无法弄清为什么它不起作用。任何建议将不胜感激。

这是我的AccountController文件

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'july_15_auth.Controllers.AccountController' 

这是我的Startup.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using july_15_auth.Models;
using july_15_auth.ViewModels;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace july_15_auth.Controllers
{
    public class AccountController : Controller
    {
            private readonly UserManager<IdentityUser> userManager;
            private readonly SignInManager<IdentityUser> signInManager;
        private readonly RoleManager<IdentityUser> roleManager;

    public AccountController(UserManager<IdentityUser> userManager,
            SignInManager<IdentityUser> signInManager, RoleManager<IdentityUser> roleManager)
        {
            this.userManager = userManager;
            this.signInManager = signInManager;
        this.roleManager = roleManager;
    }
        [AcceptVerbs("Get", "Post")]
        public async Task<IActionResult> IsEmailInUse(string email)
        {
            var user = await userManager.FindByEmailAsync(email);
            if(user == null) { return Json(true); }
            else { return Json($"Email {email} is already in use"); }
        }



            [HttpGet]
            public IActionResult Register()
            {
                return View();
            }

            [HttpPost]
            public async Task<IActionResult> Register(RegisterViewModel model)
            {
                if (ModelState.IsValid)
                {
                    // Copy data from RegisterViewModel to IdentityUser
                    var user = new IdentityUser
                    {
                        UserName = model.Email,
                        Email = model.Email
                    };

                    // Store user data in AspNetUsers database table
                    var result = await userManager.CreateAsync(user, model.Password);

                    // If user is successfully created, sign-in the user using
                    // SignInManager and redirect to index action of HomeController
                    if (result.Succeeded)
                    {
                        await signInManager.SignInAsync(user, isPersistent: false);
                        return RedirectToAction("index", "home");
                    }

                    // If there are any errors, add them to the ModelState object
                    // which will be displayed by the validation summary tag helper
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }

                return View(model);
            }

            [HttpPost]
            
            public async Task<IActionResult> Logout()
            {
            await signInManager.SignOutAsync();
            return RedirectToAction("index", "home");
            }

        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }

        // Redirect Vulnerability Fix

        [HttpPost]
        public async Task<IActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var result = await signInManager.PasswordSignInAsync(
                    model.Email, model.Password, model.RememberMe, false);

                if (result.Succeeded)
                {
                    if(!string.IsNullOrEmpty(returnUrl))
                    {
                        return LocalRedirect(returnUrl);
                    }

                    return RedirectToAction("index", "home");
                }

                ModelState.AddModelError(string.Empty, "Invalid Login Attempt");
            }

            return View(model);
        }


    }
    }

1 个答案:

答案 0 :(得分:0)

InvalidOperationException:尝试激活'july_15_auth.Controllers.AccountController'时无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager`1 [Microsoft.AspNetCore.Identity.IdentityUser]'的服务

要解决上述异常,请在向控制器中注入RoleManager实例时,尝试用RoleManager<IdentityUser>替换RoleManager<IdentityRole>

private readonly UserManager<IdentityUser> userManager;
private readonly SignInManager<IdentityUser> signInManager;
private readonly RoleManager<IdentityRole> roleManager;

public AccountController(UserManager<IdentityUser> userManager,
        SignInManager<IdentityUser> signInManager, RoleManager<IdentityRole> roleManager)
{
    this.userManager = userManager;
    this.signInManager = signInManager;
    this.roleManager = roleManager;
}

有关RoleManager<TRole>的更多信息,请检查:https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.rolemanager-1?view=aspnetcore-3.1