Add protected admin scope with login

This commit is contained in:
Francesco Lorenzo D'Amico
2026-08-18 14:56:41 +02:00
parent dfe5044c69
commit 04877691b9
7 changed files with 178 additions and 9 deletions
+11
View File
@@ -0,0 +1,11 @@
@page
@model RazorPagesMovie.Pages.Admin.IndexModel
@{
ViewData["Title"] = "Admin";
}
<h1>Welcome, @User.Identity?.Name</h1>
<form method="post" asp-page-handler="Logout">
<button type="submit" class="btn btn-secondary">Logout</button>
</form>
@@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace RazorPagesMovie.Pages.Admin;
public class IndexModel : PageModel
{
public void OnGet()
{
}
public async Task<IActionResult> OnPostLogoutAsync()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return RedirectToPage("/Login");
}
}
+28
View File
@@ -0,0 +1,28 @@
@page
@model RazorPagesMovie.Pages.LoginModel
@{
ViewData["Title"] = "Login";
}
<div class="row justify-content-center">
<div class="col-md-4">
<h1>Admin Login</h1>
@if (!string.IsNullOrEmpty(Model.ErrorMessage))
{
<div class="alert alert-danger">@Model.ErrorMessage</div>
}
<form method="post">
<div class="mb-3">
<label asp-for="Username" class="form-label"></label>
<input asp-for="Username" class="form-control" autofocus />
</div>
<div class="mb-3">
<label asp-for="Password" class="form-label"></label>
<input asp-for="Password" type="password" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
</div>
</div>
+53
View File
@@ -0,0 +1,53 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using RazorPagesMovie.Data;
using RazorPagesMovie.Models;
namespace RazorPagesMovie.Pages;
public class LoginModel : PageModel
{
private readonly RazorPagesMovieContext _context;
public LoginModel(RazorPagesMovieContext context)
{
_context = context;
}
[BindProperty]
public string Username { get; set; } = string.Empty;
[BindProperty]
public string Password { get; set; } = string.Empty;
public string? ErrorMessage { get; set; }
public void OnGet()
{
}
public async Task<IActionResult> OnPostAsync(string? returnUrl)
{
var user = await _context.AdminUser.SingleOrDefaultAsync(u => u.Username == Username);
var hasher = new PasswordHasher<AdminUser>();
if (user is null || hasher.VerifyHashedPassword(user, user.PasswordHash, Password) == PasswordVerificationResult.Failed)
{
ErrorMessage = "Benutzername oder Passwort ist falsch.";
return Page();
}
var identity = new ClaimsIdentity(
new[] { new Claim(ClaimTypes.Name, user.Username) },
CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
return LocalRedirect(returnUrl ?? "/Admin");
}
}