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

查看:132
本文介绍了.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/announcing-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/zh-cn/dotnet/standard/net-standard

除了以下依赖项之外,它的运行效果非常好:

This worked really well apart from the following dependencies:

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);

我为它找到了一个N​​uGet,但它似乎是恶意的。该软件包以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.Core中的Microsoft.AspNet.Identity.UserManager

我们保留了 UserManager 在项目之间共享。 容器来自 SimpleInjector ,它与 .NET Standard 2.0 c兼容

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.


使用
'.NETFramework,Version = v4.6.1'而不是项目恢复了软件包'EntityFramework 6.2.0'目标框架
'.NETStandard,Version = 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 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.

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对象)。

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

Microsoft.AspNetCore.Identity Microsoft.AspNetCore.Identity.EntityFrameworkCore 仅具有对 .NETStandard 2.0 的依赖关系,并且可以安装而不会发出警告。我们是否需要将ASP.NET上的Entity Framework和Identity升级到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/zh-cn/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 Presentation Foundation构建Windows
桌面应用程序的支持( WPF),
Windows窗体和Entity Framework 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-desktop-frameworks /

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

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