In NET 7 I saved Auth-Cookie via controller with
await HttpContext.SignInAsync(claims, authProperties);
Then included <CascadingAuthenticationState> in Routing. In Program.cs I then made a few settings for Cookie:
builder.Services.AddAuthentication("Cookies")
.AddCookie(options =>
{
//options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
options.ExpireTimeSpan = TimeSpan.FromDays(30);
//options.SlidingExpiration = true;
//options.AccessDeniedPath = "/Forbidden/";
});
I was able to query user authentication in every page.
[CascadingParameter]
Task<AuthenticationState> authenticationStateTask { get; set; }
var authState = await authenticationStateTask;
var user = authState.User;
// Check whether auth cookie is set/valid
if (user.Identity.IsAuthenticated)
Everything here works without problems in several Blazor Server projects.
Now the same code no longer runs under NET 8, my user.Identity is always false. I have read in a few places that the following should be added to Program.cs:
builder.Services.AddCascadingAuthenticationState();
This allows you to eliminate CascadingAuthenticationState in the routing. On the page, the user authentication is then requested as follows:
[Inject]
AuthenticationStateProvider authenticationStateProvider { get; set; }
var authState = await authenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity is not null && user.Identity.IsAuthenticated)
Source: https://learn.microsoft.com/en-us/aspnet/core/blazor/security/server/?view=aspnetcore-8.0&tabs=visual-studio
But unfortunately, my user is always false here too! Cookie is stored on the client, I checked that first.
Is this a NET 8 problem or have I not studied the documentation carefully enough and missed something important?
Thank you in advance for any help.
pc