Implementata autenticazione per tutte le pagine della dashboard

This commit is contained in:
2025-09-23 13:36:21 +02:00
parent 560fb7607f
commit 2a8f82e35d
9 changed files with 258 additions and 87 deletions

View File

@@ -4,21 +4,30 @@
<MudPopoverProvider />
<MudDialogProvider />
<MudSnackbarProvider />
<MudLayout>
<MudAppBar Elevation="1">
<MudStaticNavDrawerToggle DrawerId="nav-drawer" Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" />
<MudText Typo="Typo.h5" Class="ml-3">Application</MudText>
<MudSpacer />
<MudIconButton Icon="@Icons.Material.Filled.MoreVert" Color="Color.Inherit" Edge="Edge.End" />
</MudAppBar>
<MudDrawer id="nav-drawer" @bind-Open="_drawerOpen" ClipMode="DrawerClipMode.Always" Elevation="2">
<NavMenu />
</MudDrawer>
<MudMainContent Class="pt-16 pa-4">
@Body
</MudMainContent>
</MudLayout>
<CascadingAuthenticationState>
<MudLayout>
<MudAppBar Elevation="1">
<MudStaticNavDrawerToggle DrawerId="nav-drawer" Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" />
<MudText Typo="Typo.h5" Class="ml-3">Integry Control Panel</MudText>
<MudSpacer />
<AuthorizeView>
<Authorized>
<MudText Typo="Typo.body2" Class="mr-4">
Benvenuto, @context.User.Identity?.Name
</MudText>
</Authorized>
</AuthorizeView>
<MudIconButton Icon="@Icons.Material.Filled.MoreVert" Color="Color.Inherit" Edge="Edge.End" />
</MudAppBar>
<MudDrawer id="nav-drawer" @bind-Open="_drawerOpen" ClipMode="DrawerClipMode.Always" Elevation="2">
<NavMenu />
</MudDrawer>
<MudMainContent Class="pt-16 pa-4">
@Body
</MudMainContent>
</MudLayout>
</CascadingAuthenticationState>
<div id="blazor-error-ui" data-nosnippet>
An unhandled error has occurred.

View File

@@ -3,26 +3,33 @@
@inject NavigationManager NavigationManager
<MudNavMenu>
<MudNavLink Href="" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Home">Home</MudNavLink>
<MudNavLink Href="counter" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Add">Counter</MudNavLink>
<MudNavLink Href="weather" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Weather</MudNavLink>
<MudNavLink Href="auth" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Lock">Auth Required</MudNavLink>
<AuthorizeView>
<Authorized>
<MudNavLink Href="Account/Manage" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Person">@context.User.Identity?.Name</MudNavLink>
<form action="Account/Logout" method="post">
<AntiforgeryToken />
<input type="hidden" name="ReturnUrl" value="@currentUrl" />
<button type="submit" class="mud-nav-link mud-ripple">
<MudIcon Icon="@Icons.Material.Filled.Logout" Color="Color.Info" Class="mr-3"></MudIcon> Logout
</button>
</form>
<MudNavLink Href="dashboard" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Dashboard">Dashboard</MudNavLink>
<MudDivider Class="my-2" />
<MudNavGroup Text="Gestione" Icon="@Icons.Material.Filled.Business" Expanded="false">
<MudNavLink Href="servers" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Computer">Server</MudNavLink>
<MudNavLink Href="clients" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.People">Client</MudNavLink>
<MudNavLink Href="services" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Settings">Servizi</MudNavLink>
</MudNavGroup>
<MudNavGroup Text="Esempi" Icon="@Icons.Material.Filled.Science" Expanded="false">
<MudNavLink Href="counter" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Add">Counter</MudNavLink>
<MudNavLink Href="weather" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Weather</MudNavLink>
<MudNavLink Href="auth" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Lock">Auth Required</MudNavLink>
</MudNavGroup>
<MudDivider Class="my-2" />
<MudNavGroup Text="Account" Icon="@Icons.Material.Filled.Person" Expanded="false">
<MudNavLink Href="Account/Manage" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Person">Profilo (@context.User.Identity?.Name)</MudNavLink>
<MudNavLink Href="@($"Account/Logout?returnUrl={Uri.EscapeDataString(currentUrl ?? "/")}")"
Icon="@Icons.Material.Filled.Logout"
Style="color: var(--mud-palette-error);">
Logout
</MudNavLink>
</MudNavGroup>
</Authorized>
<NotAuthorized>
<MudNavLink Href="Account/Register" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Person">Register</MudNavLink>
<MudNavLink Href="Account/Login" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Password">Login</MudNavLink>
<MudNavLink Href="Account/Login" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Login">Login</MudNavLink>
<MudNavLink Href="Account/Register" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.PersonAdd">Register</MudNavLink>
</NotAuthorized>
</AuthorizeView>
</MudNavMenu>

View File

@@ -1,5 +1,7 @@
@page "/counter"
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
<PageTitle>Counter</PageTitle>

View File

@@ -0,0 +1,166 @@
@page "/dashboard"
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
<PageTitle>Dashboard - Integry Control Panel</PageTitle>
<MudContainer MaxWidth="MaxWidth.ExtraExtraLarge" Class="mt-4">
<MudText Typo="Typo.h3" Class="mb-4">Dashboard</MudText>
<AuthorizeView>
<Authorized>
<MudAlert Severity="Severity.Success" Class="mb-4">
Benvenuto, @context.User.Identity?.Name!
</MudAlert>
</Authorized>
</AuthorizeView>
<MudGrid>
<!-- Server Status Card -->
<MudItem xs="12" sm="6" md="4">
<MudCard>
<MudCardContent>
<div style="display: flex; justify-content: space-between; align-items: center;">
<div>
<MudText Typo="Typo.h6">Server Status</MudText>
<MudText Typo="Typo.body2" Color="Color.Success">Online</MudText>
</div>
<MudIcon Icon="@Icons.Material.Filled.Computer" Color="Color.Success" Size="Size.Large" />
</div>
</MudCardContent>
</MudCard>
</MudItem>
<!-- Active Clients Card -->
<MudItem xs="12" sm="6" md="4">
<MudCard>
<MudCardContent>
<div style="display: flex; justify-content: space-between; align-items: center;">
<div>
<MudText Typo="Typo.h6">Client Attivi</MudText>
<MudText Typo="Typo.h4" Color="Color.Primary">--</MudText>
</div>
<MudIcon Icon="@Icons.Material.Filled.People" Color="Color.Primary" Size="Size.Large" />
</div>
</MudCardContent>
</MudCard>
</MudItem>
<!-- Services Card -->
<MudItem xs="12" sm="6" md="4">
<MudCard>
<MudCardContent>
<div style="display: flex; justify-content: space-between; align-items: center;">
<div>
<MudText Typo="Typo.h6">Servizi</MudText>
<MudText Typo="Typo.h4" Color="Color.Info">--</MudText>
</div>
<MudIcon Icon="@Icons.Material.Filled.Settings" Color="Color.Info" Size="Size.Large" />
</div>
</MudCardContent>
</MudCard>
</MudItem>
<!-- Quick Actions -->
<MudItem xs="12">
<MudCard>
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h6">Azioni Rapide</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudGrid>
<MudItem xs="12" sm="6" md="3">
<MudButton Variant="Variant.Outlined"
Color="Color.Primary"
FullWidth="true"
StartIcon="@Icons.Material.Filled.Computer"
Href="/servers">
Gestisci Server
</MudButton>
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MudButton Variant="Variant.Outlined"
Color="Color.Secondary"
FullWidth="true"
StartIcon="@Icons.Material.Filled.People"
Href="/clients">
Gestisci Client
</MudButton>
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MudButton Variant="Variant.Outlined"
Color="Color.Tertiary"
FullWidth="true"
StartIcon="@Icons.Material.Filled.Settings"
Href="/services">
Configurazioni
</MudButton>
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MudButton Variant="Variant.Outlined"
Color="Color.Info"
FullWidth="true"
StartIcon="@Icons.Material.Filled.Analytics"
Href="/reports">
Report
</MudButton>
</MudItem>
</MudGrid>
</MudCardContent>
</MudCard>
</MudItem>
<!-- Recent Activity -->
<MudItem xs="12" md="8">
<MudCard>
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h6">Attivit<69> Recenti</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudText Typo="Typo.body2" Class="text-center" Color="Color.Default">
Nessuna attivit<69> recente da visualizzare
</MudText>
</MudCardContent>
</MudCard>
</MudItem>
<!-- System Info -->
<MudItem xs="12" md="4">
<MudCard>
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.h6">Informazioni Sistema</MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudStack Spacing="3">
<div>
<MudText Typo="Typo.body2">
<strong>Versione:</strong> 1.0.0
</MudText>
</div>
<div>
<MudText Typo="Typo.body2">
<strong>Database:</strong> Connesso
</MudText>
</div>
<div>
<MudText Typo="Typo.body2">
<strong>LDAP:</strong> Connesso
</MudText>
</div>
</MudStack>
</MudCardContent>
</MudCard>
</MudItem>
</MudGrid>
</MudContainer>
@code {
}

View File

@@ -1,59 +1,16 @@
@page "/"
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
@inject NavigationManager NavigationManager
<PageTitle>Home</PageTitle>
<MudText Typo="Typo.h3" GutterBottom="true">Hello, world!</MudText>
<MudText Class="mb-8">Welcome to your new app, powered by MudBlazor and the .NET 9 Template!</MudText>
<MudAlert Severity="Severity.Normal" ContentAlignment="HorizontalAlignment.Start">
You can find documentation and examples on our website here:
<MudLink Href="https://mudblazor.com" Target="_blank" Typo="Typo.body2" Color="Color.Primary">
<b>www.mudblazor.com</b>
</MudLink>
</MudAlert>
<br />
<MudText Typo="Typo.h5" GutterBottom="true">Interactivity in this Template</MudText>
<br />
<MudText Typo="Typo.body2">
When you opt for the "Global" Interactivity Location, <br />
the render modes are defined in App.razor and consequently apply to all child components.<br />
In this case, providers are globally set in the MainLayout.<br />
<br />
On the other hand, if you choose the "Per page/component" Interactivity Location,<br />
it is necessary to include the <br />
<br />
&lt;MudPopoverProvider /&gt; <br />
&lt;MudDialogProvider /&gt; <br />
&lt;MudSnackbarProvider /&gt; <br />
<br />
components on every interactive page.<br />
<br />
If a render mode is not specified for a page, it defaults to Server-Side Rendering (SSR),<br />
similar to this page. While MudBlazor allows pages to be rendered in SSR,<br />
please note that interactive features, such as buttons and dropdown menus, will not be functional.
</MudText>
<br />
<MudText Typo="Typo.h5" GutterBottom="true">What's New in Blazor with the Release of .NET 9</MudText>
<br />
<MudText Typo="Typo.h6" GutterBottom="true">Prerendering</MudText>
<MudText Typo="Typo.body2" GutterBottom="true">
If you're exploring the features of .NET 9 Blazor,<br /> you might be pleasantly surprised to learn that each page is prerendered on the server,<br /> regardless of the selected render mode.<br /><br />
This means that you'll need to inject all necessary services on the server,<br /> even when opting for the wasm (WebAssembly) render mode.<br /><br />
This prerendering functionality is crucial to ensuring that WebAssembly mode feels fast and responsive,<br /> especially when it comes to initial page load times.<br /><br />
For more information on how to detect prerendering and leverage the RenderContext, you can refer to the following link:
<MudLink Href="https://github.com/dotnet/aspnetcore/issues/51468#issuecomment-1783568121" Target="_blank" Typo="Typo.body2" Color="Color.Primary">
More details
</MudLink>
</MudText>
<br />
<MudText Typo="Typo.h6" GutterBottom="true">InteractiveAuto</MudText>
<MudText Typo="Typo.body2">
A discussion on how to achieve this can be found here:
<MudLink Href="https://github.com/dotnet/aspnetcore/issues/51468#issue-1950424116" Target="_blank" Typo="Typo.body2" Color="Color.Primary">
More details
</MudLink>
</MudText>
@code {
protected override void OnInitialized()
{
// Redirect authenticated users to dashboard
NavigationManager.NavigateTo("/dashboard", replace: true);
}
}

View File

@@ -1,6 +1,7 @@
@page "/weather"
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
<PageTitle>Weather</PageTitle>

View File

@@ -2,9 +2,26 @@
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)">
<NotAuthorized>
<RedirectToLogin />
@if (context.User.Identity?.IsAuthenticated != true)
{
<RedirectToLogin />
}
else
{
<div class="alert alert-danger">
<p>Non hai i permessi necessari per accedere a questa risorsa.</p>
</div>
}
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="typeof(Layout.MainLayout)">
<div class="alert alert-warning">
<p role="alert">Pagina non trovata!</p>
</div>
</LayoutView>
</NotFound>
</Router>

View File

@@ -1,6 +1,7 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Authorization
@using MudBlazor.StaticInput
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@@ -10,3 +11,4 @@
@using Microsoft.JSInterop
@using MudBlazor
@using MudBlazor.Services
@using IntegryControlPanel.Client

View File

@@ -49,6 +49,16 @@ namespace Microsoft.AspNetCore.Routing
return TypedResults.LocalRedirect($"~/{returnUrl}");
});
// Add GET endpoint for logout to handle navigation from WebAssembly client
accountGroup.MapGet("/Logout", async (
ClaimsPrincipal user,
[FromServices] SignInManager<ApplicationUser> signInManager,
[FromQuery] string? returnUrl) =>
{
await signInManager.SignOutAsync();
return TypedResults.LocalRedirect($"~/{returnUrl ?? "Account/Login"}");
});
var manageGroup = accountGroup.MapGroup("/Manage").RequireAuthorization();
manageGroup.MapPost("/LinkExternalLogin", async (