简体   繁体   English

<authorizeview>在 Visual Studio 中本地运行但在 IIS 上运行时不起作用?</authorizeview>

[英]<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.

相关问题 System.NullReferenceException for .NET 服务在 Windows Server IIS 上运行,但在 Visual Studio IIS Express 中运行时在本地工作 - System.NullReferenceException for .NET service running on windows server IIS but works locally when running in Visual Studio IIS Express 使用 Visual Studio 运行时查找和替换有效,但在运行 IIS 时出现空异常错误 - Find and Replace works when running with visual studio but when running off of IIS i get null exception error 在 Visual Studio 上通过 IIS Express 运行时可以加载图像,但通过 IIS 运行时无法加载 - Image can load when running by IIS Express on Visual studio, but can't load when running by IIS ASP.NET 项目在 VS 运行时在本地运行,但不在本地计算机或服务器上的 IIS 上运行 - ASP.NET project works locally on build when VS is running but not on IIS on my local machine or my server Response.Redirect()在Visual Studio中工作,但不在IIS中工作 - Response.Redirect() Works in Visual Studio but not IIS 网站适用于 Visual Studio 2017 但不适用于 IIS 10 - Website works on visual studio 2017 but not IIS 10 登录页面在 Visual Studio 中工作,而不是在 IIS 中 - Login page works in visual studio, not in IIS Visual Studio IIS Express 在启动时挂起,但 IIS 本地工作,为什么? - Visual Studio IIS Express hangs on launch, but IIS local works, why? 在没有iis或visual studio的情况下运行MVC 5网站 - Running MVC 5 website without iis or visual studio 我的asmx Web服务在本地工作,但在远程IIS 7上部署时不工作 - My asmx webservice works locally but not when deployed on remote IIS 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM