Search code examples
asp.net-coreclean-architecture

Proper place for ASP.NET Core Identity in clean architecture


There is a picture in book "Architecting modern web applications with ASP.NET Core" where ASP.NET Core Identity is located in the web layer

enter image description here

Why does belong in the Web layer?

In practice I use identity framework functionality in both the web and the infrastructure project.

As a result I use same nuget package for identity in both projects.

For example _LoginPartial.cshtml in Web project:

@inject SignInManager<AppUser> SignInManager
@inject UserManager<AppUser> UserManager
@if (SignInManager.IsSignedIn(User)) {  // ... some html }

AppIdentityDbContext.cs in Infrastructure project:

public class AppIdentityDbContext : IdentityDbContext<AppUser>
{ 
    // ... some code
}

Another example

Services like services.AddDefaultIdentity<ApplicationUser>() are added to the Infrastructure project.


Solution

  • Below are the answers to your questions:

    Why does ASP.NET Core Identity belong in the Web layer?

    ASP.NET Core Identity is located in the Web layer, as shown in the picture, because it includes components like login, registration forms, and user management pages that directly interact with the web UI. These elements belong to user interaction, which is handled by the Web layer.

    as you can see in the document it is mentioned:

    ASP.NET Core Identity

    In practice I use identity framework functionality in both the web and the infrastructure project.

    You have used AppIdentityDbContext belongs to the Infrastructure layer because it directly interacts with the database.which is not wrong. you could use ASP.NET Identity functionality in both Web and Infrastructure layers.

    For more details, you can refer to the following links: