【发布时间】:2017-01-12 18:07:04
【问题描述】:
我目前有一个面向完整 .NET Framework 的 .NET Core Web 应用程序和一个包含我的 EF6 实现的 .NET 4.6.1 类库项目。
这两个目前正在一起工作。
现在我需要添加 Identity,但我需要自定义实现。 (如果重要的话,我正在针对现有的用户表进行构建,并且只关心本地登录)
所以在 4.6.1 类库项目中,我使用本指南创建了自定义的身份类/存储/管理器:https://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity
我坚持的部分是如何配置 .NET Core 应用程序以使用非核心版本的 Identity。
所有教程都有类似的配置
services.AddIdentity<ApplicationUser, ApplicationRole>(config => { })
.AddEntityFrameworkStores<ApplicationDbContext>();
和
app.UseIdentity();
但是,这两种方法只存在于 Microsoft.AspNetCore.Identity 中,而不存在于类库使用的 Microsoft.AspNet.Identity.Core 中。
.NET Core 应用程序应该引用哪些框架? Startup.cs 配置应该是什么样的?
为了简单起见,我所有的自定义身份代码都是上面链接的文章中的内容。
Startup.cs 代码如下所示(引用了 AspNetCore.Identity)
services.AddIdentity<ApplicationUser, CustomRole>(config => { /* config */ })
.AddUserManager<ApplicationUserManager>()
.AddUserStore<CustomRoleStore>()
.AddDefaultTokenProviders();
样品控制器
public AccountController(ApplicationSignInManager signInManager)
{
_signInManager = signInManager;
}
尝试运行时出错
InvalidOperationException:ApplicationUserManager 类型必须派生自 UserManager。
【问题讨论】:
-
在尝试为这个确切的问题找到解决方案后,我放弃了,因为 AddIdentity、UserManager、SignInManager 都已实现并依赖于 AspNetCore.Identity。我相信要使这项工作与 EF6 和 EF6 Identity 一起工作,您需要推出自己的 SignInManager、RoleManager,在我的情况下,将我的项目移植到 EF Core 和 Identity Core 要简单得多。如果您只需要依赖注入,请使用 services.AddScoped 而不是 services.AddIdentity(因为 AddIdentity 在 Identity Core 中定义)。
标签: c# asp.net-identity asp.net-core-mvc