Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move identity to a class library ASP.Net Core

I have developed a new project based on ASP.Net core. I have moved all my EF code (Models,mappings, DbContext) into a dedicated DAL class library in order to follow the Single responsibility principle of the SOLID rules.

However, I need now to add authentication into my project and would need to add the following into my Startup.cs of my Web project as shown in different tutorials:

services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

It would involve adding the Microsoft.AspNetCore.Identity.EntityFrameworkCore package and it seems to me that I start breaking the SRP rule by having this package included into my Web project.

Would it possible to move all the identity code (Services, models) as an external class library as I did for the DAL.

like image 447
Sylvain C. Avatar asked Jan 13 '17 04:01

Sylvain C.


People also ask

What is ASP.NET Core identity?

ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality. Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.

What is UserManager in ASP.NET Core?

The ASP.NET Identity UserManager class is used to manage users e.g. registering new users, validating credentials and loading user information. It is not concerned with how user information is stored. For this it relies on a UserStore (which in our case uses Entity Framework).


2 Answers

Since identity code has both logic and UI (login/logout, register etc), it needs to be an web app.

There are two options IMO:

  1. Make identity as a separate web app. Since Asp.Net Core Identity supports OAuth2 (OAuth2 has support on interactive grants such as code grant), users will be redirected to this web app end point during login/register process.
  2. Combine identity controllers with yours and move identity data to your DAL library, see this: https://www.codeproject.com/Articles/1156558/ASP-NET-Core-Moving-IdentityDbContext-and-EF-model

The first option makes better option if SRP is important to you. If redirecting to a different URL seems like a bad user experience for you, then the second option may be better.

like image 113
Icerman Avatar answered Oct 19 '22 17:10

Icerman


I'm having my own research about the exact same question, found this thread you can read about the implementation here, although it is not related to .NET Core class library in particular. I believe the principal is similar and you can find your way through it. I also assume that it is not has to be implemented via a web app application as mentioned here.

like image 45
Or Assraf Avatar answered Oct 19 '22 17:10

Or Assraf