【发布时间】:2018-12-12 05:00:25
【问题描述】:
我无法部分切换到 .NET Standard。
我正在将一个类库迁移到 .NET Standard,在这个库中我有存储库和数据库通信。我已经成功迁移它以使用AspNetCore.Identity.EntityFrameworkCore。我试图实现的是最终拥有 1 个 .NET Standard 项目来处理数据库,其中 1 个 MVC .NET Framework、1 个 API .NET Framework 和 1 个新的 .NET Core 应用程序将使用它。除此之外,还有一些其他 .NET Framework 类库依赖于它。基本上,.NET Core 应用程序已经制作完成,但后端尚未“合并”以实现重叠功能。
小概述:
不将 MVC/API 转换为 .Core 的原因是目前有太多其他库依赖于 .NET Framework,有些还不能转换,但是为数据库使用相同的库是一个根本性的改变,它将避免双重实现一些存储库。
我还已经转换了实现Microsoft.AspNetCore.Identity.IdentityUser、Microsoft.AspNetCore.Identity.IdentityRole 等的实体。
所以我的 DbContext 类看起来像这样:
public class DatabaseContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken>
{
private IConfiguration _config;
public DatabaseContext(IConfiguration config) : base()
{
_config = config;
}
//all DbSet, OnModelCreating
}
我已成功运行 EFCore Code First 迁移。
现在我正在尝试在我的 MVC 应用程序中配置身份(然后也在 API 项目中)。
我只有标准的IdentityConfig.cs、Startup.Auth.cs,所有配置都已完成。我尝试查看此文档(migration identity)。我所能做的就是添加这个,AddMvc() 不存在,所以会引发编译错误:
Startup.cs
using System;
using System.IO;
using Babywatcher.Core.Data.Database;
using Babywatcher.Core.Data.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartupAttribute(typeof(MyProject.MVC.Startup))]
namespace MyProject.MVC
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var services = new ServiceCollection();
ConfigureAuth(app);
ConfigureServices(services);
}
public void ConfigureServices(IServiceCollection services)
{
// Add EF services to the services container.
services.AddDbContext<DatabaseContext>(options =>
options.UseSqlServer(""));//Configuration trying to refer to above method: Configuration.GetConnectionString("DefaultConnection")
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<DatabaseContext>()
.AddDefaultTokenProviders();
services.AddMvc();
}
}
}
好吧,我想这并没有太大的作用,而且我在两个 .NET Framework 项目中都使用 SimpleInjector,如果可能的话,我宁愿继续使用它,而不是使用默认的依赖注入器。
通过上面的添加,我真的不知道如何处理ConfigureAuth 方法以及将所有配置放在哪里。
当我尝试调整 IdentityManager 以尝试在 AspNetCore.Identity 中引用相同的类型时,我在尝试更改 ApplicationUserManager 时开始遇到问题:
创建 UserStore 不是问题,但尝试创建 UserManager 比较困难,我也尝试在我的 UserRepository 中使用它,就像我以前一样,我现在不能再使用了: (。
旧用户存储库
private readonly UserManager<ApplicationUser, string> _userManager = null;
private readonly RoleManager<ApplicationRole, string> _roleManager = null;
internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
public UserRepository(DatabaseContext dbContext) : base(dbContext, c => c.contactId, m => m.ContactId)
{
var userStore =
new UserStore<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>(dbContext);
var roleStore = new RoleStore<ApplicationRole, string, ApplicationUserRole>(dbContext);
_userManager = new UserManager<ApplicationUser, string>(userStore);
_roleManager = new RoleManager<ApplicationRole, string>(roleStore);
_userManager.UserValidator = new UserValidator<ApplicationUser>(_userManager) { AllowOnlyAlphanumericUserNames = false };
if (DataProtectionProvider == null)
{
DataProtectionProvider = new MachineKeyProtectionProvider();
}
_userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, string>(DataProtectionProvider.Create("Identity"));
}
尝试再次执行上述操作会在创建 UserManager 时出现问题,因为它会询问 9 个参数,所以我有点觉得不应该这样做......可能会做 something like this 来获得他们那里当然我首先要解决根本问题。
最后但同样重要的是:请记住,这些应用程序都已投入生产,因此尤其是在用户周围,我需要确保所有登录仍然有效。在推出此版本时,由于其他一些迁移问题,我需要将数据迁移到新数据库。
【问题讨论】:
-
我最终设法解决了这个问题,我会尽快更新答案
-
只是想知道您是如何解决这个问题的?我有一个类似的场景,我们有一个提供管理系统的旧应用程序,但我正在构建一个新的面向客户的 .net 核心 API,但需要在网络核心和网络标准之间共享身份表,并且存在一些架构差异,即LockoutEndvs LockoutEndDateUtc。到目前为止,我的代码与您上面的方法非常相似,但我还没有设法让它工作
-
@mcinnes01 啊好像忘记在这里写答案了。我今晚会写这个。虽然很遗憾我无法正确配置 DataProtectionProvider,但例如重置密码只需将令牌存储在数据库用户表中即可。我确实解决了使用身份管理器和其他东西为 .net 核心/框架使用 1 个数据库上下文的问题。
-
@mcinnes01 我的答案贴出来了,希望你能关注。
-
非常感谢,我会试试看 :)
标签: c# asp.net-mvc asp.net-core asp.net-identity .net-standard