Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Microsoft.AspNetCore.Identity in .Net Standard Project?

I am about to migrate legacy code (.Net framework) to .Net Standard (2.0, Not 2.1 so that its still compatible to Framework 4.7 and Core 3.0). The old libraries are using System.Web.Security to access the database (User and Membership etc.). I already changed that to the new Identity model with IdentityServer4.

My Question for this Issue: Can I use Microsoft.AspNetCore.Identity in a .Net Standard Project?

I read some discussion on Git and was confused. Apparently there is a difference in .Net standard 2.0 and 2.1, but NUGET allows me to install the "Microsoft.AspNetCore.Identity" for .Net Standard 2.0.

I want to know this, because this would make the migration a bit easier by "just" replacing the old classes with the new classes in Microsoft.AspNetCore.Identity. Otherwise I need to remove everything Identity related in the .Net Standard Libraries.

Unfortunately I cannot use .Net Core 3.0 for the Libraries, because the Libs need to be used in a WebForms App (.Net Framework 4.7).

Edit 1: thank you for the replies. That already helped me. But I have a follow up question:

The thing that confuses me is that I will use those Libs in a WebForms Application and I read some things about the old Frameworks not supporting the new Identity Model of aspnet core. As an example: Can I use the "ApplicationUser"-Class inside a custom .Net standard Library in the Code Behind of a WebForms app (in the case that I wrapped all User-Management in a separate .Net Standard Dll)?

Edit 2: Here is the discussion that irritated me: https://github.com/dotnet/aspnetcore/issues/3756#issuecomment-434114175 regarding the "Microsoft.AspNetCore.Identity" support in .Net Standard (even the namespace "Microsoft.AspNetCore.Identity" made me think its not supposed to work on .Net Standard)

Edit 3: So, I tried to use Microsoft.AspNetCore.Identity to use "IdentityUser" in a .Net Standard Library. Turnes out EntityFramework needs to be added in the references as well and I get this error.

Package 'EntityFramework 6.1.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.

How to fix this: For .Net Standard 2.0 install this Microsoft.Extensions.Identity.Stores. And for .Net Standard 2.1 this can be installed Microsoft.AspNetCore.Identity.EntityFrameworkCore

like image 393
user3675331 Avatar asked Nov 07 '22 10:11

user3675331


1 Answers

The .NET Standard is a specification which all .NET implementations should abide to. This was introduced for standardizing the .NET implementations.

So, if you refer the documentation, you will be able to know which .NET implementations are supported by .NET standard 2.0 and which .NET implementations are supported by .NET Standard 2.1.

Now, your question: can you use Microsoft.AspNetCore.Identity package in .NET Standard 2.0 project.

You can use it as long as the .NET implementations against which you will build your code are confirming to .NET Standard 2.0

As per this link, the Microsoft.AspNetCore.Identity v2.2.0 is targetd to .NET Standard 2.0, hence you would be able to use this in your project.

Please note .NET Framework won't support .NET Standard 2.1 or later versions. For more details, see the announcement of .NET Standard 2.1.

like image 79
Manoj Choudhari Avatar answered Nov 12 '22 18:11

Manoj Choudhari