NET 8: Auth-Cookie and CascadingAuthenticationState

perfect code 276 Reputation points
2023-12-10T20:26:00.3033333+00:00

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

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Blazor
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. perfect code 276 Reputation points
    2023-12-12T18:39:18.6833333+00:00

    I came up with a solution. The following link helped me: https://www.youtube.com/watch?v=B3zvX_CWKVc

    However, I have made some changes and implemented some different things than explained in the video:

    • I don't use controllers but a minimal API
    • in Routes.razor I have no <CascadingAuthenticationState>, but add in Program.cs builder.Services.AddCascadingAuthenticationState();
    • On the Pages I have
    [Inject] 
    AuthenticationStateProvider authenticationStateProvider { get; set; }
    
    var authState = await authenticationStateProvider.GetAuthenticationStateAsync();                 bool IsAuthenticated = authState.User.Identity.IsAuthenticated;                 
    var user = authState.User;                  
    if (user.Identity is not null && user.Identity.IsAuthenticated)
    {
    ...
    }
    

    pc

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.