我们将客户端的Web应用程序从.NET 4.6升级到.NET 6。旧系统使用身份2和EF 6。这两个系统都需要在不确定的时间内共存。
首先,如果不覆盖旧系统中的密码,我们无法从新系统中登录,从而使其无法使用。我们用:
services.Configure<PasswordHasherOptions>(option => option.CompatibilityMode = PasswordHasherCompatibilityMode.IdentityV2);
然后遵循概述此stackoverflow文章有脚本可以更新sql表并告诉我们如何包括 Microsoft.aspnet.Identity.CoreCompat 库。但是,在 lockoutend 字段中, aspnetusers 表中似乎不起作用。脚本将此字段作为DateTime2字段留下。但是,在.NET 6中运行时,我们会收到错误:
System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.DateTimeOffset'.
将SQL中的字段更改为DateTimeOffset(7)允许Identity .NET 6工作,但是打破身份2,因为它期望类型DateTime并获得DateTimeOffset。
如果字段为null,则没有问题。但是,如果该领域有日期,则任何访问用户的东西,例如 FindbyNameSync 都会丢弃错误。
必须是一种工作的方法。我无法想象我们是唯一遇到这个问题的人。我不知道其他所有人会如何滑倒。
有人对此有解决方案吗?我是否缺少其他配置设置?
We are upgrading a client's web app from .NET 4.6 to .Net 6. The old system uses Identity 2 and EF 6. Both systems need to coexist for an indefinite period of time.
At first we could not log in from the new system without it overwriting the passwords in the old system, making them unusable. We fixed that with:
services.Configure<PasswordHasherOptions>(option => option.CompatibilityMode = PasswordHasherCompatibilityMode.IdentityV2);
Then we followed the path outlined this StackOverflow article which had scripts to update the SQL tables and tells us how to include the Microsoft.AspNet.Identity.CoreCompat library. However, it seems to not work when it comes to the LockoutEnd field in the AspNetUsers table. The scripts leave this field as a datetime2 field. However, when running in .NET 6, we get the error:
System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.DateTimeOffset'.
Changing the field in SQL to a DateTimeOffset(7) allows Identity .NET 6 to work, but breaks Identity 2 because it expects type DateTime and got DateTimeOffset.
If the field is null, it works no problem. However, if there was a date in the field, anything accessing the user, like FindByNameAsync will throw the error.
There must be a way for this to work. I can't imagine we are the only ones having this problem. I have no idea how this could have slipped by everyone else.
Does anyone have a solution to this? Is there some other config setting I am missing?
发布评论