1

I created a new project using a .NET 9 Blazor Web App template. And followed the documentation for adding the authentication state provider.

My understanding from the documentation is that the all I need to do is add builder.Services.AddCascadingAuthenticationState(); to my Program.cs which I did.

I am not using a custom AuthenticationStateProvider.

Then I should simply be able to cascade the authentication state into my page. [CascadingParameter] private Task<AuthenticationState>? authenticationState { get; set; }

Then await during my OnInitializedAsync() to receive my current users identity.

protected override async Task OnInitializedAsync() { if (authenticationState is null) { authMessage = "Unable to determine authentication state."; return; } var authState = await authenticationState; var user = authState?.User; if (user?.Identity is not null && user.Identity.IsAuthenticated) { authMessage = $"{user.Identity.Name} is authenticated."; } else { authMessage = "The user is NOT authenticated."; } } 

However IsAuthenticated always equals false. This is problematic because I have a two .NET 7 Blazor Server apps that were upgraded to .NET 8 that use cascading authentication. And they are able to receive the authenticated user via the authentication state.

What I have tried is adding more services to in my Program.cs

builder.Services.AddAuthorization(); builder.Services.AddAuthorizationCore(); builder.Services.AddCascadingAuthenticationState(); 

This had no effect.

I ensured the launchSettings.json Windows authentication is true and anonymous is false to match my other projects.

"iisSettings": { "windowsAuthentication": true, "anonymousAuthentication": false, "iisExpress": { "applicationUrl": "http://localhost:50496", "sslPort": 44397 } 

I tried wrapping the Routes.razor with the cascading authentication state. Which if my understanding of the documentation is correct. Shouldn't be necessary.

<Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState> <Router AppAssembly="typeof(Program).Assembly"> <Found Context="routeData"> <RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" /> <FocusOnNavigate RouteData="routeData" Selector="h1" /> </Found> </Router> </Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState> 

I then also tried wrapping the App.razor in the cascading authentication state. I tried this because this is where in my other functional apps the cascading authentication state is located.

<Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <base href="/" /> <HeadOutlet @rendermode="@InteractiveServer" /> </head> <body class="k-body"> <Routes @rendermode="@InteractiveServer" /> <script src="_framework/blazor.web.js" autostart="false"></script> <script> document.addEventListener("DOMContentLoaded", function() { Blazor.start(); }); </script> </body> </html> </Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState> 

I expected that one of these changes would lead to user.Identity.IsAuthenticated to be true. But I am still unable to successfully configure my projects authentication state.

However I can retrieve the Name value if I use this method. var userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; This variation works albeit only for Windows systems.

1 Answer 1

0

You could follow this document to set up windows authentication in your app:

Add the NuGet package Microsoft.AspNetCore.Authentication.Negotiate

add authentication & authorization services:

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme) .AddNegotiate(); builder.Services.AddAuthorization(options => { options.FallbackPolicy = options.DefaultPolicy; }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.