Context.User正在改变奇怪空的Signalr [英] Context.User is changing to null strangely on Signalr

查看:263
本文介绍了Context.User正在改变奇怪空的Signalr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

在2.0.2这被证实错误

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

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

您的选择是:

  1. 使用永久框架或长轮询(问题只发生在网络插座使用)
  2. 以code从github上,并建立自己的二进制文件修复包括
  3. 在等待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:

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

这篇关于Context.User正在改变奇怪空的Signalr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆