简体   繁体   English

Blazor 应用程序不在 IIS 上维护其状态,但在 IISExpress 上运行良好

[英]Blazor app not maintainintg its state on IIS, but works fine on IISExpress

I have an issue where I am trying to obtain the context userID and display it.我有一个问题,我试图获取上下文用户 ID 并显示它。 This works great on IISExpress from within VS but has an issue on IIS.这在 VS 中的 IISExpress 上效果很好,但在 IIS 上存在问题。

When the page first loads, it displays the username properly.当页面首次加载时,它会正确显示用户名。 Then a second later it shows the "not authorized" message.然后一秒钟后,它显示“未授权”消息。 I am assuming this is an issue with pre-render/postback of some kind that doesn't happen in VS, but I'm at a loss as to what is causing this.我假设这是在 VS 中不会发生的某种预渲染/回发的问题,但我不知道是什么原因造成的。 :

@inherits LayoutComponentBase
@inject IHttpContextAccessor httpContextAccessor
@inject AuthenticationStateProvider GetAuthenticationStateAsync
@inject Data.LoginState loginState

<PageTitle>Tools</PageTitle>

<div class="page">
    <div class="sidebar">
        <NavMenu />
    </div>

    <main>
        <div class="top-row px-4">
            @if (loginState.IsLoggedIn.Equals(false))
            {
                <h5>You are not authorized</h5>
            }
            else{
                <h5>Welcome: @loginState.username</h5>
            }
        </div>

        <article class="content px-4">
            @if (loginState.IsLoggedIn.Equals(false)){}
            @Body

        </article>
    </main>
</div>

@code{
    string? username = String.Empty;
    protected override void OnInitialized()
    {        
        try
        {
            username = (httpContextAccessor.HttpContext.User.Identity.Name).Substring(httpContextAccessor.HttpContext.User.Identity.Name.IndexOf(@"\") + 1);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        if (!String.IsNullOrEmpty(username))
        {
            loginState.SetLoginAsync(true, username);
        }
    }
}

LoginState is in Program.cs LoginState 在 Program.cs 中

builder.Services.AddScoped<LoginState>();

This is using Windows Authentication这是使用 Windows 身份验证

Also I get the standard:我也得到了标准:

Warning: Failed to connect via WebSockets警告:无法通过 WebSockets 连接

even with the appsettings.config entry line即使使用 appsettings.config 入口行

I have resolved this issue.我已经解决了这个问题。 It requires Windows feature Web Sockets to be installed and then suddenly pre-render or whatever was causing it no longer breaks the functionality.它需要安装 Windows 功能 Web 套接字,然后突然预渲染或任何导致它不再破坏功能的东西。

Web Sockets网络套接字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Blazor Webassembly EditForm 在 IISExpress 上工作,但在 IIS 上有问题 - Blazor Webassembly EditForm works on IISExpress but has Problem on IIS IIS 和 IISExpress 与 COM 库 - IIS and IISExpress with COM libraries 在 AWS 上部署 blazor 服务器应用程序“错误:如果连接不在‘已连接’State 中,则无法发送数据。” 但适用于本地主机 - Deployed blazor server app on AWS “Error: Cannot send data if the connection is not in the 'Connected' State.” But works on local host 将站点上载到IIS,找不到控制器资源(错误500)。 使用iisexpress在本地工作 - Uploaded site to IIS, cannot find controller resource (error 500). Works locally using iisexpress Xaml IIS 中的渲染不起作用(但在 IISExpress 中) - Xaml Rendering in IIS not working (but in IISExpress) 与 AD 的连接在 IIS 中工作正常,但在 VS 中却不行 - Connection to AD works fine in IIS but not in VS 将 Blazor 应用程序部署到 IIS 子应用程序 - Deploy Blazor app to a IIS sub application WebApplication 在 IIS Express 上运行良好,但在本地 IIS 上不起作用 - WebApplication works fine on IIS Express but it doesn't on Local IIS IIS6 应用程序池 state - IIS6 App Pool state 您可以在子文件夹中的 IIS 上托管 blazor 应用程序吗? - Can you host a blazor app on IIS in a sub folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM