Changes on Database for Rolebased Users also started with Login Cookies.

This commit is contained in:
2025-07-05 00:15:51 +02:00
parent c385ee0628
commit 1152bc4f7e
11 changed files with 575 additions and 250 deletions

View File

@@ -24,8 +24,26 @@ namespace WatchLog
builder.Services.AddHttpContextAccessor();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
builder.Services.AddIdentityCore<AppUser>(options =>
{
// Hier könntest du Passwortregeln festlegen, z.B.
options.Password.RequireDigit = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequiredLength = 4; // Nur für Entwicklung!
})
.AddSignInManager() // Fügt den SignInManager hinzu, der den Login-Prozess steuert.
.AddDefaultTokenProviders(); // Nötig für Features wie Passwort-Reset.
// 2. Jetzt sagen wir Identity, welche Klassen es für seine Aufgaben verwenden soll.
// Dies ist der wichtigste Teil!
builder.Services.AddScoped<IUserStore<AppUser>, MyUserStore>();
builder.Services.AddScoped<IPasswordHasher<AppUser>, PasswordHasher<AppUser>>();
// 3. Da wir IdentityCore verwenden, müssen wir die Cookie-Authentifizierung selbst hinzufügen.
// Die Konfiguration ist fast identisch zu deiner alten, aber sie ist jetzt
// an das Identity-System gekoppelt.
builder.Services.AddAuthentication(IdentityConstants.ApplicationScheme)
.AddCookie(IdentityConstants.ApplicationScheme, options =>
{
options.Cookie.Name = "WatchLogAuthCookie";
options.LoginPath = "/login";
@@ -35,13 +53,12 @@ namespace WatchLog
options.SlidingExpiration = true;
});
// 4. Die Autorisierungs-Policy ist perfekt und bleibt genau so!
// Sie sorgt dafür, dass alle Seiten standardmäßig einen Login erfordern.
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
});
builder.Services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
builder.Services.AddScoped<IPasswordHasher<Admin>, PasswordHasher<Admin>>();
var app = builder.Build();