Microsoft.AspNet.Identity and Microsoft.AspNet.Identity.EntityFramework in .NET Standard 2.0
Asked Answered
N

1

7

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://learn.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://mcmap.net/q/406741/-net-core-equivalent-of-callcontext-logicalget-setdata

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

Nordau answered 9/11, 2018 at 11:37 Comment(11)
this EntityFramework 6.2.0' was restored using '.NETFramework,Version=v4.6.1 will not start under linux, so if you need to run your website under linux, you need to switch from EF to EF coreStopping
@Stopping Yes that is why we can't risk having it :)Nordau
so, what the question then? :)Stopping
@Stopping Do we need to upgrade Entity Framework and Identity to Core or is there another way to get it working with .NET Standard? :)Nordau
.NET Standard 2.0 is supported on the following platforms, so if you are using FW 4.6.1 - you can use .NET Standard 2.0 librariesStopping
Incomplete question on my side. We would like to have the functionality in the .NET Standard Library and it will be used by both Framework AND Core.Nordau
I have 2 applications - one is WPF with FW 4.6.2, another one is asp.net core 2.1 - both of them using same .NET Standard 2.0 libraries with no problemStopping
But then you don’t have IdentityUser in Standard 2.0 lib? See point 3 and 4 aboveNordau
yep, I dont have IdentityUser, it is from .NET Core which is not compatible with FW 4.6.1 (afaik), so you cannot share it across project. I dont get why you wrote about SimpleInjector and how it is related to question, but you can only share .NET Standard libraries, and cannot share .NET Core with full framework.Stopping
@Stopping SimpleInjector was to describe what the inherited Container was in the code example. The text is long but if you read it all I think it is pretty clear what the problem is and where our problems are.Nordau
@Nordau did you get it working in the end?Boxhaul
N
0

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/

Nordau answered 20/2, 2019 at 7:53 Comment(2)
How did EntityFramework solve that exactly? You cannot add a reference to to EntityFramework from a .net standard project...Feces
@Feces Yes you can. EntityFramework 6.4.4 is compatible with .NETStandard 2.1. nuget.org/packages/EntityFramework#dependencies-body-tab They did not solve the .NET Framework compatibility though since no .NET Framework is compatible with .NETStandard 2.1. learn.microsoft.com/en-us/dotnet/standard/…Nordau

© 2022 - 2024 — McMap. All rights reserved.