简体   繁体   English

尝试激活“AuthenticateController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务

[英]Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthenticateController'

I'm getting this error in Login Controller.我在登录 Controller 中收到此错误。

System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[UsersAuth.IdentityAuth.UserApplication]' while attempting to activate 'UsersAuth.Controllers.AuthenticateController'. System.InvalidOperationException:尝试激活“UsersAuth.Controllers.AuthenticateController”时,无法解析“Microsoft.AspNetCore.Identity.UserManager`1[UsersAuth.IdentityAuth.UserApplication]”类型的服务。

here is Authenticate Controller constructor:这是 Authenticate Controller 构造函数:

public class AuthenticateController : ControllerBase
    {
        private readonly UserManager<UserApplication> _userManager;
        private readonly RoleManager<IdentityRole> _roleManager;
        private readonly IConfiguration _configuration;

        public AuthenticateController(UserManager<UserApplication> userManager, RoleManager<IdentityRole> roleManager, IConfiguration configuration)
        {
            _userManager = userManager;
            _roleManager = roleManager;
            _configuration = configuration;
        }

and here is ConfigureServices in startup.cs:这是startup.cs中的ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    services.AddDbContext<ApplicationDbContext>(options=>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
     })
     .AddJwtBearer(options =>
     {
         options.SaveToken = true;
         options.RequireHttpsMetadata = false;
         options.TokenValidationParameters = new TokenValidationParameters()
         {
             ValidateIssuer = true,
             ValidateAudience = true,
             ValidIssuer = Configuration["JWT:ValidIssuer"],
             ValidAudience = Configuration["JWT:ValidAudience"],
             IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:SecretKey"]))
         };
     });
}

You didn't configure Dependency Injection for UserApplication .您没有为UserApplication配置依赖注入。 There are different ways to do that, for example有不同的方法可以做到这一点,例如

services.AddDefaultIdentity<UserApplication>(options =>
   options.SignIn.RequireConfirmedAccount = true)
   .AddEntityFrameworkStores<ApplicationDbContext>();

or, if you want more granular control,或者,如果您想要更精细的控制,

services.AddIdentityCore<AppUser>(opt => {
    opt.Password.RequiredLength = 8;
    ...
})
.AddSignInManager()
.AddRoles<IdentityRole>();

I would comment but I don't have rep yet to do it so here it goes: Is UserManager class your creation?我会发表评论,但我还没有代表这样做,所以这里是:UserManager class 是您的创作吗? I don't see a call registering it in IServiceCollection to be resolved when constructing your controller.在构建您的 controller 时,我没有看到在 IServiceCollection 中注册它的调用要解决。

An example registration for you UserManager could look like this:您的 UserManager 注册示例可能如下所示:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    services.AddScoped<UserManager<UserApplication>>();

    // other registrations follow
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试激活“AuthController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthController' 尝试激活“Management.Controllers.RoleController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'Management.Controllers.RoleController' 尝试激活“ Controllers.AccountsController”时无法解析类型为“ Microsoft.AspNetCore.Identity.UserManager”的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager hile attempting to activate 'Controllers.AccountsController' 尝试激活“WebShop.Controllers.User.UserController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'WebShop.Controllers.User.UserController' 无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager 如何解决“无法解析类型为‘Microsoft.AspNetCore.Identity.UserManager’的服务”? - How to solve "Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager"? 尝试激活“xxxxx.LoginModel”时无法解析“Microsoft.AspNetCore.Identity.SignInManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager` while attempting to activate 'xxxxx.LoginModel' Microsoft.AspNetCore.Identity.UserManager`1[M.Application.Models.ApplicationUserModel]' 尝试激活 M.Application.Services.EPDService - Microsoft.AspNetCore.Identity.UserManager`1[M.Application.Models.ApplicationUserModel]' while attempting to activate M.Application.Services.EPDService 没有类型“Microsoft.AspNetCore.Identity.UserManager”的服务:在尝试扩展 IdentityUser 时? - No service for type 'Microsoft.AspNetCore.Identity.UserManager' : while trying to extend IdentityUser? 没有注册“Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]”类型的服务 - No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM