c# - .NET Standard 2.0 中的 Microsoft.AspNet.Identity 和 Microsoft.AspNet.Identity.EntityFramework

标签 c# asp.net .net .net-standard .net-standard-2.0

背景:我们正在进行的项目包含多个共享两个库的解决方案。今天一切都是用 .NET Framework 4.6.1 编写的。该项目的目标是为新项目采用 .NET Core 并能够在 Docker 中运行 Web 应用程序。

随着 .NET Standard 2.1 的新发布以及 .NET Framework 4.8 将保留在 .NET Standard 2.0 而不是实现 .NET Standard 2.1 感觉是时候开始了。 Microsoft 的 Immo Landwerth 是这样说的:

But what is also true is that the rate of innovation in .NET Framework has to slow down in order to reduce breakage. In that sense, you should generally expect that most new features will only become available on .NET Core (and derived platforms, such as Xamarin, Mono, and Unity as they build from the same sources as .NET Core).

https://blogs.msdn.microsoft.com/dotnet/2018/11/05/announcing-net-standard-2-1/

我们希望在我们的新项目中使用新功能,但不想将每个旧项目都转换为 .NET Core。为了保持 .NET Framework.NET Core 之间的兼容性,我们决定将我们的共享库转换为 .NET Standard 2.0

https://learn.microsoft.com/en-us/dotnet/standard/net-standard

除了以下依赖项之外,这确实工作得很好:

<强>1。 System.Net.Http.WebRequestHandler - 已解决

用于这样的客户端证书:

WebRequestHandler requestHandler = new WebRequestHandler();

//Add certificate if setting exists
if (!string.IsNullOrEmpty(pushEvent?.CertificateThumbprint?.Thumbprint))
{
    var certificate = certificateService.GetClientCertificate(pushEvent?.CertificateThumbprint?.Thumbprint);
    if (certificate != null)
    {
        requestHandler.ClientCertificates.Add(certificate);
    }
}

var client = new HttpClient(requestHandler);

我为它找到了一个 NuGet,但它似乎是恶意的。该包链接到 Microsoft 文档作为 Project Site,并将 Microsoft 拼错为作者 Microsfot。报告它以便 Microsoft 可以查看它。

https://www.nuget.org/packages/WebRequest.WebRequestHandler/

不过,我们似乎可以将 WebRequestHandler 更改为 HttpClientHandler 以使其开箱即用。

<强>2。 System.Runtime.Remoting.Messaging -> CallContext.LogicalGetData - 已解决

在这里解决:https://stackoverflow.com/a/53211839/3850405

<强>3。 Microsoft.AspNet.Identity.EntityFramework.IdentityUser

我们有一个继承自 IdentityUser 的用户模型。

public class AppUser : IdentityUser, ICurrentUser
{
    public bool LocalEnvironment { get; set; }

    public Guid? TokenId { get; set; }
}

<强>4。来自程序集 Microsoft.AspNet.Identity.Core 的 Microsoft.AspNet.Identity.UserManager

我们让我们的 UserManager 在项目之间共享。 Container 来自SimpleInjector,兼容.NET Standard 2.0

public class AppUserManager : UserManager<AppUser>
{

    public AppUserManager(IUserStore<AppUser> store)
        : base(store)
    {

    }

    public static AppUserManager Create<AppContainer>() where AppContainer : Container, new()
    {
        var container = new AppContainer();
        var store = container.GetInstance<IUserStore<AppUser>>();

        var manager = new AppUserManager(store);

        manager.UserValidator = new UserValidator<AppUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false
        };


        return manager;
    }
}

如果共享库中安装了 EntityFramework NuGet,则会出现以下警告。我们不能冒险。

Package 'EntityFramework 6.2.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.

我已经了解了为什么他们将 IdentityUser 放在 EF 库中,IdentityUser 非常特定于 EF。然而,它使得移植到 .NET Standard 2.0. 变得更加困难。

Why is the IdentityUser class in the Microsoft.AspNet.Identity.EntityFramework namespace and not in the Core package?

我还了解到 ASP.NET Core 2.0 已经删除了基础 IdentityUser POCO(普通旧 CLR 对象)。

Microsoft.AspNetCore.IdentityMicrosoft.AspNetCore.Identity.EntityFrameworkCore 仅依赖于 .NETStandard 2.0 并且无需安装警告。我们是否需要将 ASP.NET 上的 Entity Framework 和 Identity 升级到 Core,或者是否有其他方法让它与 .NET Standard 一起工作?我们需要让它运行的最后一步。

https://learn.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x?view=aspnetcore-2.1

https://learn.microsoft.com/en-us/ef/efcore-and-ef6/side-by-side

最佳答案

鉴于我们只需要 EntityFramework 6.2.0 即可同时使用 .NET Framework.NET Core 这将在 .NET 核心 3

.NET Core 3 is a major update which adds support for building Windows desktop applications using Windows Presentation Foundation (WPF), Windows Forms, and Entity Framework 6 (EF6).

https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-3-preview-1-and-open-sourcing-windows-desktop-frameworks/

关于c# - .NET Standard 2.0 中的 Microsoft.AspNet.Identity 和 Microsoft.AspNet.Identity.EntityFramework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53225028/

相关文章:

c# - 单声道 : Set a program to launch at startup for alternative OS

c# - VS2017 扫描数据

c# - Azure Active Directory 访问错误

c# - 您可以使用 ITextSharp (c#) 打印 pdf 文件吗?如果是如何?

c# - .Net Standard 2.0 dll 的 .Net 45 序列化

.net - C# shell 扩展

.net - 如何让企业库配置和项目设置设计器在app.config中引用相同的值

c# - 如何向从 MVC3 Razor View 调用的函数添加 <T> 类型参数?

c# - 如何将 BitmapImage 图像转换为数据,以便我可以通过 Windows Phone 7 的 HTTP POST 请求将其上传到 Web?

c# - 网页如何自动识别国家?