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


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)