.NET Standard 2.0 中的 Microsoft.AspNet.Identity 和 Microsoft.AspNet.Identity.EntityFramework

Ogg*_*las 6 .net c# asp.net .net-standard .net-standard-2.0

背景:我们正在开展的项目由多个共享两个库的解决方案组成。一切都写在.NET Framework 4.6.1今天。该项目的目标是采用.NET Core新项目并能够在Docker.

随着新版本的发布.NET Standard 2.1以及.NET Framework 4.8将保留.NET Standard 2.0而不是实施的事实,.NET Standard 2.1感觉是开始的合适时机。微软的 Immo Landwerth 说:

但同样正确的是 .NET Framework 的创新速度必须放慢以减少损坏。从这个意义上说,您通常应该期望大多数新功能只会在 .NET Core(以及派生平台,例如 Xamarin、Mono 和 Unity,因为它们从与 .NET Core 相同的源构建)上可用。

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

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

https://docs.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);
Run Code Online (Sandbox Code Playgroud)

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

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

但是,似乎我们可以更改WebRequestHandlerHttpClientHandler使其开箱即用。

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; }
}
Run Code Online (Sandbox Code Playgroud)

4. Microsoft.AspNet.Identity.UserManager 来自程序集 Microsoft.AspNet.Identity.Core

我们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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

包“EntityFramework 6.2.0”是使用“.NETFramework,Version=v4.6.1”而不是项目目标框架“.NETStandard,Version=v2.0”恢复的。此包可能与您的项目不完全兼容。

我已经读过他们为什么放入IdentityUserEF 库,这IdentityUser是非常特定于EF 的。然而,它使移植.NET Standard 2.0.变得更加困难。

为什么 IdentityUser 类在 Microsoft.AspNet.Identity.EntityFramework 命名空间中而不是在 Core 包中?

我还读到ASP.NET Core 2.0已经删除了基础IdentityUserPOCO(Plain Old CLR Object)。

Microsoft.AspNetCore.Identity并且Microsoft.AspNetCore.Identity.EntityFrameworkCore只依赖于.NETStandard 2.0并且可以在没有警告的情况下安装。我们是否需要将 ASP.NET 上的 Entity Framework 和 Identity 升级到 Core,还是有其他方法可以使用它.NET Standard?我们需要让它运行的最后一步。

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

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

Ogg*_*las 0

鉴于我们只需要EntityFramework 6.2.0同时使用两者.NET Framework.NET Core这将在.NET Core 3.

.NET Core 3 是一项重大更新,增加了对使用 Windows Presentation Foundation (WPF)、Windows Forms 和 Entity Framework 6 (EF6) 构建 Windows 桌面应用程序的支持。

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