From 3fc53550c021635d13a88e8e36e50236d472e222 Mon Sep 17 00:00:00 2001 From: Thomas Mildner Date: Fri, 5 May 2023 14:17:59 +0200 Subject: [PATCH 1/2] - add page reload to nav menu - add refresh button - add cookie policy --- CookieAuthenticationExample/App.razor | 2 + .../Pages/FetchData.razor | 102 ++++++++++-------- CookieAuthenticationExample/Program.cs | 9 +- .../Shared/NavMenu.razor | 15 ++- 4 files changed, 76 insertions(+), 52 deletions(-) diff --git a/CookieAuthenticationExample/App.razor b/CookieAuthenticationExample/App.razor index 6cfc765..d0fac67 100644 --- a/CookieAuthenticationExample/App.razor +++ b/CookieAuthenticationExample/App.razor @@ -5,6 +5,8 @@ @*this gets displayed if the user is not authorized to view the page*@

Sie sind nicht berechtigt, diese Seite aufzurufen.

+ +
diff --git a/CookieAuthenticationExample/Pages/FetchData.razor b/CookieAuthenticationExample/Pages/FetchData.razor index cbc9e68..2c85edf 100644 --- a/CookieAuthenticationExample/Pages/FetchData.razor +++ b/CookieAuthenticationExample/Pages/FetchData.razor @@ -4,6 +4,7 @@ @using System.Security.Claims @inject AuthenticationStateProvider AuthenticationStateProvider @inject WeatherForecastService ForecastService +@inject NavigationManager NavigationManager @attribute [Authorize] @@ -13,65 +14,72 @@

This component demonstrates fetching data from a service.

+ + @if (forecasts == null) { -

- Loading... -

+

+ Loading... +

} else { - - - - - - - - - - - @foreach (var forecast in forecasts) - { - - - - - - - } - -
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
+ + + + + + + + + + + @foreach (var forecast in forecasts) + { + + + + + + + } + +
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
} @code { - private WeatherForecast[]? forecasts; - private IEnumerable claims = Enumerable.Empty(); - private IEnumerable userClaimRoles = Enumerable.Empty(); + private WeatherForecast[]? forecasts; + private IEnumerable claims = Enumerable.Empty(); + private IEnumerable userClaimRoles = Enumerable.Empty(); + + private async Task GetClaimsPrincipalData() + { + var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + var user = authState.User; + if (user.Identity is not null && user.Identity.IsAuthenticated) + { + claims = user.Claims; + userClaimRoles = user.Claims.Select(a => a.Value).ToList(); + } - private async Task GetClaimsPrincipalData() - { - var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - var user = authState.User; - if (user.Identity is not null && user.Identity.IsAuthenticated) - { - claims = user.Claims; - userClaimRoles = user.Claims.Select(a => a.Value).ToList(); - } + if (userClaimRoles.Contains("Administrator")) + { + //yayyy admin + } + } - if (userClaimRoles.Contains("Administrator")) - { - //yayyy admin - } - } + private async Task Refresh() + { + NavigationManager.NavigateTo("/fetchdata", true); + } - protected override async Task OnInitializedAsync() - { - //dummy call to simulate claim user role check - GetClaimsPrincipalData(); + protected override async Task OnInitializedAsync() + { + //dummy call to simulate claim user role check + GetClaimsPrincipalData(); - forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now)); - } + forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now)); + } } \ No newline at end of file diff --git a/CookieAuthenticationExample/Program.cs b/CookieAuthenticationExample/Program.cs index e969f4d..ad15455 100644 --- a/CookieAuthenticationExample/Program.cs +++ b/CookieAuthenticationExample/Program.cs @@ -14,11 +14,18 @@ builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { - options.ExpireTimeSpan = TimeSpan.FromSeconds(30); + options.ExpireTimeSpan = TimeSpan.FromSeconds(5); options.SlidingExpiration = true; options.AccessDeniedPath = "/Forbidden"; options.LoginPath = "/login"; + options.Cookie = new CookieBuilder() + { + SameSite = SameSiteMode.Lax, + SecurePolicy = CookieSecurePolicy.SameAsRequest, + HttpOnly = true, + }; + }); builder.Services.AddSingleton(); diff --git a/CookieAuthenticationExample/Shared/NavMenu.razor b/CookieAuthenticationExample/Shared/NavMenu.razor index cac7f66..53ead44 100644 --- a/CookieAuthenticationExample/Shared/NavMenu.razor +++ b/CookieAuthenticationExample/Shared/NavMenu.razor @@ -1,4 +1,6 @@ -