[英]<AuthorizeView> works when running locally in Visual Studio but not on IIS?
I created a new Blazor server side application using Visual Studio 2019. I selected Windows authentication.我使用 Visual Studio 2019 创建了一个新的 Blazor 服务器端应用程序。我选择了 Windows 身份验证。
The following component ( \BlazorApp1\BlazorApp1\Shared\LoginDisplay.razor
) displays "Hello, Domain\Username."以下组件 ( \BlazorApp1\BlazorApp1\Shared\LoginDisplay.razor
) 显示“Hello, Domain\Username”。 on the top right of the page in the browser in the application.在应用程序浏览器页面的右上角。
<AuthorizeView>
Hello, @context.User.Identity.Name!
</AuthorizeView>
Then I publish it to IIS on Windows Server 2019 R2 in the same Windows domain (Company intranet network).然后我将它发布到同一 Windows 域(公司内部网络)中的 Windows Server 2019 R2 上的 IIS。 However, browsing the page doesn't show the message in?但是,浏览页面并没有显示消息?
How is the client authentication information passed to the Blazer server?客户端认证信息是如何传递给Blazer服务器的?
There is good example on Microsoft docs for getting authentication user details: Microsoft 文档上有一个很好的示例,用于获取身份验证用户的详细信息:
@page "/"
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider
<button @onclick="@LogUsername">Write user info to console</button>
@code {
private async Task LogUsername()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
Console.WriteLine($"{user.Identity.Name} is authenticated.");
}
else
{
Console.WriteLine("The user is NOT authenticated.");
}
}
}
To answer回答
How is the client authentication information passed to the Blazer server?客户端认证信息是如何传递给Blazer服务器的?
If authentication state data is required for procedural logic, such as when performing an action triggered by the user, obtain the authentication state data by defining a cascading parameter of type Task:如果过程逻辑需要认证状态数据,比如执行用户触发的动作时,通过定义一个Task类型的级联参数来获取认证状态数据:
@page "/"
<button @onclick="@LogUsername">Log username</button>
@code {
[CascadingParameter]
private Task<AuthenticationState> authenticationStateTask { get; set; }
private async Task LogUsername()
{
var authState = await authenticationStateTask;
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
Console.WriteLine($"{user.Identity.Name} is authenticated.");
}
else
{
Console.WriteLine("The user is NOT authenticated.");
}
}
}
for details refer:详情请参考:
https://learn.microsoft.com/en-us/aspnet/core/security/blazor/?view=aspnetcore-3.0&tabs=visual-studio https://learn.microsoft.com/en-us/aspnet/core/security/blazor/?view=aspnetcore-3.0&tabs=visual-studio
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.