Context.User正在改变奇怪空的Signalr奇怪、Context、User、Signalr

2023-09-05 00:05:16 作者:南笙

在SignalR,我遇到Context.User突然变成空值,也有时是完全空,但这不应该发生,因为只有授权用户才能访问的枢纽。

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

  [授权]
公共类FeedHub:集线器
{
    公众覆盖任务OnConnected()
    {
        VAR名称= Context.User.Identity.Name; //这里是用户不为空
        VAR用户=的getUser(); //但它更改为null,则此私有方法的内部

        返回base.OnConnected();
    }

    私人用户的getUser()
    {
        VAR名称= Context.User.Identity.Name; //这里是用户属性为null,并抛出异常
        返回null; //
    }


    公众覆盖任务OnDisconnected()
    {
        //在这里Context.User财产有时是零,但在我看来,这不应该是空
        //因为轮毂被授权属性的保护。

        返回base.OnDisconnected();
    }
  }
 

解决方案 如何将其他盘分一部分空间给C盘 xp系统

在2.0.2这被证实错误

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

目前它解决了,但不包括在正式发布。

您的选择是:

使用永久框架或长轮询(问题只发生在网络插座使用) 以code从github上,并建立自己的二进制文件修复包括 在等待2.0.3。应该有解决的。

In SignalR, i experience that Context.User is suddenly turning into null value and also it is sometimes completely null but this should never be happen because only Authorized users can access the hub.

What is reason of these strange behaviors ? I am using SignalR 2.0 with ASP.NET MVC 4 on Visual Studio 2013.

[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();
    }
  }

解决方案

This is confirmed bug in 2.0.2

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

Currently it is resolved but not included in official release.

Your options are:

Use Forever Frame or Long Polling (issue happens only when web sockets are used) Take code from github and build your own binaries with fix included Wait for 2.0.3. It should be resolved there.