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

查看:14
本文介绍了ASP.NET Core Identity 不注入 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,没有任何更改)——首先做 DB 似乎是个坏主意.

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;

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. 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 [命名空间.模型.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 Identity 不注入 UserManager&lt;ApplicationUser&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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