AddIdentityApiEndpoints<TUser>() is not available in the Infrastructure layerWhen trying to use AddIdentityApiEndpoints<TUser>() in the Infrastructure layer, you might encounter errors because this method is specifically designed for Minimal APIs in .NET 8 and is not accessible from class libraries like Infrastructure. In other side, you may notice that you can access to AddIdentityCore<TUser>() or AddIdentity<TUser>() methods with only the package Microsoft.AspNetCore.Identity.EntityFrameworkCore installed in your Domain or Infrastructrue layer.
AddIdentityCore<TUser>() is part of the Microsoft.AspNetCore.Identity package and is available in all ASP.NET Core projects, including class libraries like Infrastructure.
AddIdentityApiEndpoints<TUser>(), however, is part of the Minimal API feature in .NET 8 and is only available in ASP.NET Core Web Applications. It depends on the Microsoft.AspNetCore.App framework, which isn't included in class libraries.
To maintain a clean architecture, you should:
In the Infrastructure layer, configure Identity Core and Entity Framework.
services.AddIdentityCore<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
In your project web Api layer, configure the API endpoints (via AddIdentityApiEndpoints).
builder.Services.AddIdentityApiEndpoints<IdentityUser>();
AddIdentityApiEndpoints<TUser>() can only be used in ASP.NET Core Web Applications.
Infrastructure should only handle the service configuration, while Web API layer should expose the endpoints.
This maintains a clean and modular architecture.