gpt4 book ai didi

c# - Context.User 在 Signalr 上奇怪地变为 null

转载 作者:太空狗 更新时间:2023-10-29 23:39:12 25 4
gpt4 key购买 nike

在 SignalR 中,我发现 Context.User 突然变成 null 值,而且它有时完全为 null,但这永远不会发生,因为只有授权用户才能访问中心。

这些奇怪行为的原因是什么?我在 Visual Studio 2013 上使用 SignalR 2.0 和 ASP.NET MVC 4。

[Authorize]
public class FeedHub : Hub
{
public override Task OnConnected()
{
var name = Context.User.Identity.Name;// here is User is not null
var user = GetUser();// but it is changing to null inside this private method

return base.OnConnected();
}

private User GetUser()
{
var name = Context.User.Identity.Name;// here is User property is null and throws exception
return null;//
}


public override Task OnDisconnected()
{
//In here Context.User property is sometimes null but in my opinion this should never be null
// because Hub is protected by Authorize attribute.

return base.OnDisconnected();
}
}

最佳答案

这是 2.0.2 中确认的错误

https://github.com/SignalR/SignalR/issues/2753

目前已解决但未包含在正式版本中。

您的选择是:

  1. 使用 Forever Frame 或 Long Polling(问题仅在使用 web sockets 时发生)
  2. 从 github 获取代码并构建您自己的包含修复程序的二进制文件
  3. 等待 2.0.3。它应该在那里解决。

关于c# - Context.User 在 Signalr 上奇怪地变为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20461161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com