admin管理员组文章数量:1393052
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.
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
Reset to default 0You 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;
});
本文标签: aspnet coreNET 9 Blazor Web App CascadingCascadingAuthenticationState always falseStack Overflow
版权声明:本文标题:asp.net core - .NET 9 Blazor Web App CascadingCascadingAuthenticationState always false - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744739355a2622517.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论