.NET Standard 2.0 中的 Microsoft.AspNet.Identity 和 Microsoft.AspNet.Identity.EntityFramework [英] Microsoft.AspNet.Identity and Microsoft.AspNet.Identity.EntityFramework in .NET Standard 2.0

查看:34
本文介绍了.NET Standard 2.0 中的 Microsoft.AspNet.Identity 和 Microsoft.AspNet.Identity.EntityFramework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Background: The project we are wokring on consists of several solutions that share two libraries. Everything is written in .NET Framework 4.6.1 today. A goal for the project has been to adopt .NET Core for new projects and being able to run web applications in Docker.

随着 .NET Standard 2.1 的新版本以及 .NET Framework 4.8 将保留在 .NET Standard 2.0 而不是的事实实施 .NET Standard 2.1 感觉是时候开始了.微软的 Immo Landwerth 说:

With the new release of .NET Standard 2.1 and the fact that .NET Framework 4.8 will remain on .NET Standard 2.0 rather than implement .NET Standard 2.1 it felt like the right time to start. Immo Landwerth from Microsoft says this:

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

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/annoucing-net-standard-2-1/

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

We would like to have access to new features in our new projects but do not want to convert every old project to .NET Core. To keep compatibility between .NET Framework and .NET Core we decided to convert our shared libraries to .NET Standard 2.0.

https://docs.microsoft.com/en-us/dotnet/标准/网络标准

除了以下依赖项外,这非常有效:

This worked really well apart from the following dependencies:

1.System.Net.Http.WebRequestHandler - 已解决

用于像这样的客户端证书:

Used for client certificates like this:

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,但它似乎是恶意的.该包作为 Project Site 链接到 Microsoft 文档,并将 Microsoft 拼错为作者 Microsfot.报告它以便 Microsoft 可以查看它.

I found a NuGet for it but it seems malicious. The package links to Microsoft documentation as Project Site and has misspelled Microsoft as author, Microsfot. Reported it so Microsoft can have a look at it.

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

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

However it seems we can change WebRequestHandler to HttpClientHandler to get it working out of the box.

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

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

3.Microsoft.AspNet.Identity.EntityFramework.IdentityUser

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

We have a User Model that inherits from IdentityUser.

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

    public Guid? TokenId { get; set; }
}

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

我们保持我们的 UserManager 在项目之间共享.Container 来自 SimpleInjector,它与 .NET Standard 2.0 兼容.

We keep our UserManager shared between the projects. Container is from SimpleInjector which is compatible with .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 安装在共享库中,则会出现以下警告.我们不能冒那个险.

If the EntityFramework NuGet is installed in the shared library the warning below is present. We can't risk that.

EntityFramework 6.2.0"包已使用'.NETFramework,Version=v4.6.1' 而不是项目目标框架'.NETStandard,版本 = v2.0'.此软件包可能不完全兼容与您的项目.

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. 变得更加困难.

I have read about why they put IdentityUser in the EF Library, IdentityUser is very EF specific. However it makes porting to .NET Standard 2.0. harder.

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

我还读到 ASP.NET Core 2.0 删除了基本的 IdentityUser POCO(Plain Old CLR Object).

I have also read that ASP.NET Core 2.0 have removed the base IdentityUser POCO (Plain Old CLR Object).

Microsoft.AspNetCore.IdentityMicrosoft.AspNetCore.Identity.EntityFrameworkCore 仅依赖于 .NETStandard 2.0 并且无需安装即可安装警告.我们是否需要将 ASP.NET 上的实体框架和标识升级到 Core,还是有其他方法可以让它与 .NET Standard 一起工作?我们需要让它运行的最后一步.

Microsoft.AspNetCore.Identity and Microsoft.AspNetCore.Identity.EntityFrameworkCore only has dependencies to .NETStandard 2.0 and can be installed without a warning. Do we need to upgrade Entity Framework and Identity on ASP.NET to Core or is there another way to get it working with .NET Standard? Last step that we need to get it running.

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

推荐答案

鉴于我们只需要 EntityFramework 6.2.0 即可使用 .NET Framework>.NET Core 这将在 .NET Core 3 中解决.

Given that we only need EntityFramework 6.2.0 to work with both .NET Framework and .NET Core this will be solved in .NET Core 3.

.NET Core 3 是一项重大更新,增加了对构建 Windows 的支持使用 Windows Presentation Foundation (WPF) 的桌面应用程序,Windows 窗体和实体框架 6 (EF6).

.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-桌面框架/

这篇关于.NET Standard 2.0 中的 Microsoft.AspNet.Identity 和 Microsoft.AspNet.Identity.EntityFramework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆