12

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.

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Sylvain C.
  • 1,043
  • 1
  • 11
  • 27
  • 3
    Yes you can. Move that to an extension method in the external library and call it at the composition root, which would be the web project. – Nkosi Jan 13 '17 at 04:56
  • Following since I am attempting to do the same but haven't figured it out. – Reza Feb 05 '17 at 19:45

3 Answers3

1

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.

Icerman
  • 1,099
  • 4
  • 14
  • 30
1

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.

Or Assraf
  • 564
  • 1
  • 4
  • 12
  • 6
    Please don't post answers that consist largely of references to external sites. Those sites might go away, and with them, the substance of your response. Synthesize what the external resource has to offer the original poster. – chb Apr 06 '19 at 00:34
-4

yes you can just install Microsoft.AspNetCore.Identity.EntityFrameworkCore into class library

Kamran Ul Haq
  • 23
  • 1
  • 4