Microsoft.AspNet.Identity and Microsoft.AspNet.Identity.EntityFramework in .NET Standard 2.0

ⅰ亾dé卋堺 提交于 2019-12-07 21:12:45

问题


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.

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:

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/

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/standard/net-standard

This worked really well apart from the following dependencies:

1. System.Net.Http.WebRequestHandler - Solved

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

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/

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

2. System.Runtime.Remoting.Messaging -> CallContext.LogicalGetData - Solved

Solved here: https://stackoverflow.com/a/53211839/3850405

3. Microsoft.AspNet.Identity.EntityFramework.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 from assembly Microsoft.AspNet.Identity.Core

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

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

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.

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?

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

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


回答1:


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 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/



来源:https://stackoverflow.com/questions/53225028/microsoft-aspnet-identity-and-microsoft-aspnet-identity-entityframework-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!