[英]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 套接字,然后突然预渲染或任何导致它不再破坏功能的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.