ASP.NET Core身份不会注入UserManager< ApplicationUser>. [英] ASP.NET Core Identity does not inject UserManager<ApplicationUser>

查看:585
本文介绍了ASP.NET Core身份不会注入UserManager< ApplicationUser>.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个较旧的asp.net核心身份数据库,我想为其映射一个新项目(一个Web API).

I've got an older asp.net core identity database, and I want to map a new project (a web api) to it.

仅出于测试目的,我复制了Models文件夹和上一个项目的ApplicationUser文件(ApplicationUser只是从IdentityUser继承,没有任何更改)-首先做数据库似乎是个坏主意.

Just for the test, I copied the Models folder, and the ApplicationUser file from the previous project (ApplicationUser simply inherits from IdentityUser, no changes whatsoever) - doing DB first seems to be a bad idea.

我正在ConfigureServices中注册身份(但我没有将其添加到管道中,因为我唯一的意图是使用UserStore)

I'm registering Identity in ConfigureServices (but I'm not adding it to the pipeline since my only intention is to use the UserStore)

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

我的期望是现在

     UserManager<ApplicationUser>

...应自动注入到构造函数中.

...should be automatically injected into constructors.

但是,将以下代码添加到控制器时 私人UserManager _userManager;

However, when adding the following code to a controller private UserManager _userManager;

    public UserController(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }

...每次对api的调用都以一个异常结束: HttpRequestException:响应状态代码未指示成功:500(内部服务器错误).

... every call to the api ends with an exception: HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error).

删除注入"代码将使运行稳定的Web api可以接受请求.

Removing the "injection" code results in smoothly running web api that can accept requests.

很难调试,因为这会在到达我的任何代码之前发生.知道为什么会这样吗?

It's hard to debug as this occurs before any of my code is reached. Any idea why this is occurring?

P.S.从例外设置"窗口启用所有例外后,我得到了以下信息:

P.S. After enabling all exceptions from the Exception Settings window I got this one:

抛出异常:
中的'System.InvalidOperationException' Microsoft.Extensions.DependencyInjection.dll

Exception thrown: 'System.InvalidOperationException' in
Microsoft.Extensions.DependencyInjection.dll

其他信息:无法解析类型的服务 尝试激活时出现"Namespace.Data.ApplicationDbContext" 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4 [Namespace.Models. ApplicationUser,Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole,Namespace.Data.ApplicationDbContext,System.String]'.

Additional information: Unable to resolve service for type 'Namespace.Data.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[Namespace.Models. ApplicationUser,Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole,Namespace.Data.ApplicationDbContext,System.String]'.

推荐答案

Configure方法中是否有app.UseIdentity();调用:

 public void Configure(IApplicationBuilder app, 
                       IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        /*...*/
        app.UseIdentity();
       /*...*/          
    }

编辑 您还在services.AddIdentity<ApplicationUser, IdentityRole>()行之前也有此行吗?

EDIT Do you also have this line before the services.AddIdentity<ApplicationUser, IdentityRole>() line?

 public void ConfigureServices(IServiceCollection services)
 {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
             options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

 }

这应该可以正常工作.另外,请检查ApplicationDbContext是否从IdentityDbContext继承.

This should work OK. Also please check if ApplicationDbContext inherits from IdentityDbContext.

这篇关于ASP.NET Core身份不会注入UserManager&lt; ApplicationUser&gt;.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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