login works, claims können angezeigt werden

This commit is contained in:
2025-06-14 18:31:33 +02:00
parent 3d96f55e28
commit d5ea5e4581
8 changed files with 190 additions and 62 deletions

View File

@@ -0,0 +1,38 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Watcher.Controllers;
public class AuthController : Controller
{
public IActionResult Login()
{
return View();
}
public IActionResult SignIn()
{
return Challenge(new AuthenticationProperties
{
RedirectUri = "/"
}, "oidc");
}
public IActionResult Logout()
{
return SignOut("Cookies", "oidc");
}
[Authorize]
public IActionResult Info()
{
var name = User.Identity?.Name;
var claims = User.Claims.Select(c => new { c.Type, c.Value }).ToList();
ViewBag.Name = name;
ViewBag.Claims = claims;
return View();
}
}

View File

@@ -1,9 +1,11 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Watcher.Models;
namespace Watcher.Controllers;
[Authorize]
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;

View File

@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Watcher.Data;
var builder = WebApplication.CreateBuilder(args);
@@ -8,6 +9,9 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
// HttpContentAccessor
builder.Services.AddHttpContextAccessor();
// ---------- Konfiguration laden ----------
var configuration = builder.Configuration;
@@ -22,23 +26,27 @@ builder.Services.AddDbContext<AppDbContext>(options =>
// ---------- Authentifizierung konfigurieren ----------
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie()
.AddOpenIdConnect(options =>
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://pocketid.triggermeelmo.com";
options.ClientId = "your-client-id";
options.ClientSecret = "your-client-secret";
var config = builder.Configuration.GetSection("Authentication:PocketID");
options.Authority = config["Authority"];
options.ClientId = config["ClientId"];
options.ClientSecret = config["ClientSecret"];
options.ResponseType = "code";
options.CallbackPath = config["CallbackPath"];
options.SaveTokens = true;
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name", // oder "preferred_username" oder der Wert, den du im Schritt 1 gesehen hast
};
options.Scope.Add("openid");
options.Scope.Add("profile");
// Optional: NameClaim & RoleClaim
options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.RoleClaimType = "role";
});

View File

@@ -0,0 +1,15 @@
@{
ViewData["Title"] = "Account Info";
}
<h2>Account-Informationen</h2>
<p><strong>Benutzername:</strong> @ViewBag.Name</p>
<h4>Claims:</h4>
<ul>
@foreach (var claim in ViewBag.Claims)
{
<li><strong>@claim.Type:</strong> @claim.Value</li>
}
</ul>

View File

@@ -0,0 +1,9 @@
@{
ViewData["Title"] = "Login";
}
<h2>Willkommen beim Watcher</h2>
<p>Bitte melde dich über PocketID an:</p>
<a class="btn btn-primary" href="/Account/SignIn">Mit PocketID anmelden</a>

View File

@@ -1,13 +1,6 @@
@{
ViewData["Title"] = "Home Page";
ViewData["Title"] = "Dashboard";
}
<head>
<link rel="stylesheet" href="css/site.css" />
</head>
<body>
<div class="text-center"></div>
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
</body>
<h1>Dashboard</h1>
<p>Willkommen im Watcher Monitoring Interface!</p>

View File

@@ -1,49 +1,103 @@
<!DOCTYPE html>
<html lang="en">
@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor HttpContextAccessor
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - Watcher</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/Watcher.styles.css" asp-append-version="true" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
<style>
body {
overflow: hidden;
}
.sidebar {
width: 240px;
height: 100vh;
position: fixed;
left: 0;
top: 0;
background-color: #343a40;
color: white;
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 1rem;
}
.main {
margin-left: 240px;
padding: 2rem;
}
.nav-link {
color: #adb5bd;
}
.nav-link:hover {
color: white;
}
.account-box {
font-size: 0.9rem;
color: #ced4da;
}
</style>
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container-fluid">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Watcher</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
<div class="sidebar">
<div>
<h4>Watcher</h4>
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link" href="/">Dashboard</a>
</li>
<li class="nav-item"></li>
<a class="nav-link" href="/Uptime">Uptime</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/Server">Server</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/Container">Container</a>
</li>
</ul>
</div>
<div class="account-box mt-auto">
@if (User.Identity?.IsAuthenticated ?? false)
{
<a href="/Auth/Info" class="d-block text-decoration-none text-light">
<div class="d-flex align-items-center gap-2">
<div class="rounded-circle bg-secondary text-white px-2 py-1">
<i class="bi bi-person"></i>
</div>
<div>
<strong>@User.Identity.Name</strong><br />
<small class="text-muted">Profil ansehen</small>
</div>
</div>
</a>
}
else
{
<a class="nav-link p-0 text-primary" href="/Account/Login">Login</a>
}
</div>
</div>
<footer class="border-top footer text-muted">
<div class="container">
&copy; 2025 - Watcher - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@await RenderSectionAsync("Scripts", required: false)
<main class="main">
@RenderBody()
</main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

View File

@@ -8,5 +8,14 @@
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "server=192.168.178.68;port=3306;database=watcher;user=monitoringuser;password=ssp123;"
}
},
"Authentication": {
"PocketID": {
"Authority": "https://pocketid.triggermeelmo.com",
"ClientId": "629a5f42-ab02-4905-8311-cc7b64165cc0",
"ClientSecret": "QHUNaRyK2VVYdZVz1cQqv8FEf2qtL6QH",
"CallbackPath": "/signin-oidc",
"ResponseType": "code"
}
}
}