<small id='1ThsE'></small><noframes id='1ThsE'>

  1. <i id='1ThsE'><tr id='1ThsE'><dt id='1ThsE'><q id='1ThsE'><span id='1ThsE'><b id='1ThsE'><form id='1ThsE'><ins id='1ThsE'></ins><ul id='1ThsE'></ul><sub id='1ThsE'></sub></form><legend id='1ThsE'></legend><bdo id='1ThsE'><pre id='1ThsE'><center id='1ThsE'></center></pre></bdo></b><th id='1ThsE'></th></span></q></dt></tr></i><div id='1ThsE'><tfoot id='1ThsE'></tfoot><dl id='1ThsE'><fieldset id='1ThsE'></fieldset></dl></div>

      <tfoot id='1ThsE'></tfoot>

      • <bdo id='1ThsE'></bdo><ul id='1ThsE'></ul>
      <legend id='1ThsE'><style id='1ThsE'><dir id='1ThsE'><q id='1ThsE'></q></dir></style></legend>

      ASP.NET Core Identity 不注入 UserManager&lt;ApplicationUser

      ASP.NET Core Identity does not inject UserManagerlt;ApplicationUsergt;(ASP.NET Core Identity 不注入 UserManagerlt;ApplicationUsergt;)
      <i id='6iDVc'><tr id='6iDVc'><dt id='6iDVc'><q id='6iDVc'><span id='6iDVc'><b id='6iDVc'><form id='6iDVc'><ins id='6iDVc'></ins><ul id='6iDVc'></ul><sub id='6iDVc'></sub></form><legend id='6iDVc'></legend><bdo id='6iDVc'><pre id='6iDVc'><center id='6iDVc'></center></pre></bdo></b><th id='6iDVc'></th></span></q></dt></tr></i><div id='6iDVc'><tfoot id='6iDVc'></tfoot><dl id='6iDVc'><fieldset id='6iDVc'></fieldset></dl></div>

    1. <legend id='6iDVc'><style id='6iDVc'><dir id='6iDVc'><q id='6iDVc'></q></dir></style></legend>

        <tbody id='6iDVc'></tbody>
        <bdo id='6iDVc'></bdo><ul id='6iDVc'></ul>

            <tfoot id='6iDVc'></tfoot>
            1. <small id='6iDVc'></small><noframes id='6iDVc'>

              1. 本文介绍了ASP.NET Core Identity 不注入 UserManager&lt;ApplicationUser&gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个较旧的 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;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                <i id='YrLG9'><tr id='YrLG9'><dt id='YrLG9'><q id='YrLG9'><span id='YrLG9'><b id='YrLG9'><form id='YrLG9'><ins id='YrLG9'></ins><ul id='YrLG9'></ul><sub id='YrLG9'></sub></form><legend id='YrLG9'></legend><bdo id='YrLG9'><pre id='YrLG9'><center id='YrLG9'></center></pre></bdo></b><th id='YrLG9'></th></span></q></dt></tr></i><div id='YrLG9'><tfoot id='YrLG9'></tfoot><dl id='YrLG9'><fieldset id='YrLG9'></fieldset></dl></div>

                      <tbody id='YrLG9'></tbody>

                        • <bdo id='YrLG9'></bdo><ul id='YrLG9'></ul>
                          <tfoot id='YrLG9'></tfoot>
                        • <legend id='YrLG9'><style id='YrLG9'><dir id='YrLG9'><q id='YrLG9'></q></dir></style></legend>
                        • <small id='YrLG9'></small><noframes id='YrLG9'>