Blazor App Server Windows 身份验证在发布后不起作用

Blazor App Server Windows authentication not working after publish

我想创建一个测试应用程序,它只是自动向用户授权 Windows 身份验证。目的是测试并了解基本 Windows 身份验证的工作原理,以及我可以用它做什么。

当我在 Visual Studio 2019 年启动应用程序时,身份验证有效。但是当我发布应用程序并在本地启动它时,它不再起作用。我不再通过身份验证了。

我创建了一个新的 Blazor 应用程序,服务器端使用身份验证方法 Windows 身份验证并使用以下代码覆盖了 FetchData.razor 页面。我从 Microsoft Docs

复制了这个
@page "/fetchData"
@using System.Security.Claims
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider

<h3>ClaimsPrincipal Data</h3>

<button @onclick="GetClaimsPrincipalData">Get ClaimsPrincipal Data</button>

<p>@_authMessage</p>

@if (_claims.Count() > 0)
{
    <ul>
        @foreach (var claim in _claims)
        {
            <li>@claim.Type: @claim.Value</li>
        }
    </ul>
}

<p>@_surnameMessage</p>

@code {
    private string _authMessage;
    private string _surnameMessage;
    private IEnumerable<Claim> _claims = Enumerable.Empty<Claim>();

    private async Task GetClaimsPrincipalData()
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;

        if (user.Identity.IsAuthenticated)
        {
            _authMessage = $"{user.Identity.Name} is authenticated.";
            _claims = user.Claims;
            _surnameMessage =
                $"Surname: {user.FindFirst(c => c.Type == ClaimTypes.Surname)?.Value}";
        }
        else
        {
            _authMessage = "The user is NOT authenticated.";
        }
    }
}

当我在 Visual Studio 2019 年在我的本地计算机上执行此操作时,它运行良好。我可以按下按钮,我通过了身份验证,我的所有声明也会被列出。 然后我清理解决方案,重新构建然后发布它没有任何错误。

我在我的本地计算机上启动完全发布的应用程序,按下按钮,它告诉我我没有通过身份验证。我不明白,因为发生了变化,它是一个已发布的应用程序。

我检查了我的 launchSettings -> windowsAuthentication 是 True,anonymousAuthentication 是 False,这是应该的。

我在网上没有找到任何东西,我希望有人知道导致问题的原因

感谢您的帮助

当您将应用程序发布到 IIS 时,请确保 Windows 在 web.config 中指定了身份验证。对于 IIS,它看起来像:

  <system.webServer>
    <security>
      <authentication>
        <windowsAuthentication enabled="true" />
        <anonymousAuthentication enabled="false" />
      </authentication>
    </security>
  </system.webServer>

查看 MS 文档:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-3.1&tabs=visual-studio