We’ll use the in-memory database for this example.
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.InMemory dotnet add package Swashbuckle.AspNetCore.Filters
class MyUser : IdentityUser {}
public class DataContext : IdentityDbContext<MyUser> { public DataContext(DbContextOptions<DataContext> options) : base(options) { } }
Program.cs
using Microsoft.EntityFrameworkCore; using Swashbuckle.AspNetCore.Filters; using WebApplication1; using WebApplication1.Data; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(options => { options.AddSecurityDefinition("oauth2", new Microsoft.OpenApi.Models.OpenApiSecurityScheme { In = Microsoft.OpenApi.Models.ParameterLocation.Header, Name = "Authorization", Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey }); options.OperationFilter<SecurityRequirementsOperationFilter>(); }); builder.Services.AddDbContext<DataContext>(options => options.UseInMemoryDatabase("AppDb")); builder.Services.AddAuthentication(); builder.Services.AddIdentityApiEndpoints<MyUser>() .AddEntityFrameworkStores<DataContext>(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.MapIdentityApi<MyUser>(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();
References
https://www.youtube.com/watch?v=8J3nuUegtL4
https://devblogs.microsoft.com/dotnet/whats-new-with-identity-in-dotnet-8/