Add authentication and identity support using ASP.NET Core Identity

This commit is contained in:
2026-01-23 21:28:16 +01:00
parent cec2b65c29
commit 534ec5f36f
9 changed files with 807 additions and 9 deletions

View File

@@ -1,5 +1,7 @@
using API.Database;
using API.Models.Internal.User;
using API.Repository.AgeGroup;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var webRoot = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..", "wwwroot"));
@@ -14,6 +16,14 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Authentication
builder.Services.AddAuthorization();
builder.Services.AddAuthentication().AddCookie(IdentityConstants.ApplicationScheme);
builder.Services.AddIdentityCore<User>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddApiEndpoints();
// Database
var postgreConnection = builder.Configuration.GetConnectionString("PostgresConnection");
if (!string.IsNullOrEmpty(postgreConnection))
@@ -57,6 +67,9 @@ app.UseHttpsRedirection();
app.UseAuthorization();
// Map Auth Endpoints
app.MapIdentityApi<User>();
app.MapControllers();
// Frontend Fallback
app.MapFallbackToFile("index.html");