ASP.NET MVC 4 中缺少 Session 变量

Missing Session variable in ASP.NET MVC 4

提问人:Sam 提问时间:11/17/2023 更新时间:11/17/2023 访问量:19

问:

我遇到了缺少会话的问题。经过几天的调查,我发现当它尝试从 localhost 调用 SignalR 的 AJAX 发布触发器时,会话丢失了。

我有一个本地 Windows 服务,该服务安装在托管我的网站(ASP.NET IIS 中托管的 MVC 4)的同一 VM 上。它将执行一些任务,完成后,它会通过 https://localhost/api/notify 发送通知。这反过来又使用 SignalR 来广播消息。

当客户端浏览器收到此消息时,它将对 /CPanel/OptiTasks/RefreshData 执行 AJAX 回调(客户端将通过外部 URL 启动页面 - 例如:https://example.com)。但是,当我尝试检索 Session 变量时,它消失了。

我试图将withCredentials设置为true,但Session变量仍然消失了。

总而言之,顺序如下:

1. Client login to the website via external URL (eg. https://example.com)
2. After login the client goes to page https://example.com/cpanel/optitasks
3. On the OptiTasks page, it uses SignalR to listen to the update notification
4. Windows Service --- Send Notification Via --> https://localhost/api/notify
5. SignalR will broadcast a notification request which will received by the OptiTask page
6. OptiTask page will do AJAX post (withCredentials = true) to /cpanel/optitasks/refreshdata
7. In RefreshDataController, the Session variable is NULL (eg. Session["MySessionVariable"] is NULL)

我已经在网上搜索了好几天,包括使用 ChatGPT,但我仍然无法弄清楚如何解决这个问题。

请帮忙...

asp.net-mvc-4 signalr 变量会话 状态

评论

0赞 Sam 11/26/2023
大家好,我解决了这个问题。我正在使用 的 SessionState 模式。问题出在 Windows 服务上,它使用 ServerManager 对服务器配置进行各种更改(例如更新 SSL 证书),这导致 IIS 回收应用程序池StateServer

答: 暂无答案