【问题标题】:<AuthorizeView> works when running locally in Visual Studio but not on IIS?<AuthorizeView> 在 Visual Studio 中本地运行但在 IIS 上运行时有效?
【发布时间】:2020-03-24 14:29:03
【问题描述】:

我使用 Visual Studio 2019 创建了一个新的 Blazor 服务器端应用程序。我选择了 Windows 身份验证。

以下组件 (\BlazorApp1\BlazorApp1\Shared\LoginDisplay.razor) 显示“Hello, Domain\Username!”在应用程序中浏览器页面的右上角。

<AuthorizeView>
    Hello, @context.User.Identity.Name!
</AuthorizeView>

然后我将它发布到 Windows Server 2019 R2 上的 IIS,位于同一 Windows 域(公司 Intranet 网络)中。但是,浏览页面不显示消息?

客户端身份验证信息如何传递到 Blazer 服务器?

【问题讨论】:

  • 我可能错了,但它在本地工作,因为服务器和浏览器在同一个域/网络中运行。我猜想稍后部署该应用程序的服务器和您从中访问该应用程序的浏览器是不同的。不是吗?
  • 它们都在同一个 Windows 域中。
  • 您是否为您的 IIS 服务器启用了 Windows 身份验证并禁用了匿名身份验证?仅当我启用 Windows 身份验证时才会显示该消息。如果我只是启用匿名身份验证,即使“你好”也不会显示
  • 我有同样的问题 - AuthorizeView 在 windows auth 下的 IIS 上不起作用,但在本地起作用

标签: asp.net iis windows-authentication blazor


【解决方案1】:

在 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.");
        }
    }
}

回答

客户端身份验证信息如何传递给 Blazer 服务器?

如果过程逻辑需要鉴权状态数据,比如执行用户触发的动作时,通过定义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.");
        }
    }
}

详情参考:

https://docs.microsoft.com/en-us/aspnet/core/security/blazor/?view=aspnetcore-3.0&tabs=visual-studio

【讨论】:

  • 您没有回答问题... OP 想知道身份验证状态如何路由到 Blazor 应用程序,而不是如何在 Blazor 应用程序中使用它。她想知道它是如何在 Windows 身份验证的上下文中完成的。您的回答和文档没有提供任何有用的信息。
猜你喜欢
  • 2020-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多