Dotnet Core 8 - Authentication/Authorization Template
In order to get very easily authentication & authorization in a .Net Core 8 project you have to follow a few steps as follows:
- Add swagger authentication
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Core8Auth", Version = "v1" });
// Add a Bearer token input box in Swagger UI
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme.",
Type = SecuritySchemeType.Http,
Scheme = "bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
});
- Add authentication & authorization support
builder.Services.AddIdentityApiEndpoints<IdentityUser>().AddEntityFrameworkStores<AppDbContext>();
- Add support for your favorite database we'll use for the purpose of this template Sqlite (also define the connection string in appsettings.json)
```c#
builder.Services.AddDbContext<AppDbContext>(o => o.UseSqlite("name=sqLite"));
- Add support for the identity controller
app.MapGroup("/identity").MapIdentityApi<IdentityUser>();
- Configure the repository (notice we are inheriting from IdentityDbContext)
public class AppDbContext : IdentityDbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
}
- Install dotnet ef tool latest version (8.0.0)
dotnet ef migrations add InitialCreate
dotnet ef database update
dotnet build
dotnet run