11

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.

2
  • 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, 2017 at 4:56
  • Following since I am attempting to do the same but haven't figured it out.
    – Reza
    Feb 5, 2017 at 19:45

3 Answers 3

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.

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.

1
  • 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 6, 2019 at 0:34
-4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.