Backend: - UserRecord Model + Users-Tabelle in DB - AuthService: SHA256-Passwort-Hashing, Token-Verwaltung (ConcurrentDictionary) - AuthMiddleware: Schützt alle /api/-Routes (außer /api/auth/login) - AuthEndpoint: Login, Logout, Me - Default-User admin/admin wird automatisch erstellt Frontend: - Login-Page mit Formular (Enter-Key-Support) - Globaler Fetch-Interceptor: 401 → automatisch Login-Redirect - Auth-Check beim App-Start und bei Hash-Änderungen - Logout-Link im Dashboard-Header - Login-CSS: Zentrierte Login-Box
41 lines
971 B
C#
41 lines
971 B
C#
using System.Threading.Tasks;
|
|
using Gott.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Gott.Middleware;
|
|
|
|
public class AuthMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
|
|
public AuthMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext ctx, AuthService auth)
|
|
{
|
|
var path = ctx.Request.Path.Value ?? "";
|
|
|
|
// Static files and login endpoint don't require auth
|
|
if (!path.StartsWith("/api/") || path == "/api/auth/login")
|
|
{
|
|
await _next(ctx);
|
|
return;
|
|
}
|
|
|
|
var token = ctx.Request.Cookies["gott_token"];
|
|
var userId = auth.ValidateToken(token);
|
|
|
|
if (userId == null)
|
|
{
|
|
ctx.Response.StatusCode = 401;
|
|
await ctx.Response.WriteAsJsonAsync(new { error = "Not authenticated" });
|
|
return;
|
|
}
|
|
|
|
ctx.Items["UserId"] = userId;
|
|
await _next(ctx);
|
|
}
|
|
}
|