Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context.User is changing to null strangely on Signalr

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();
    }
  }
like image 550
Freshblood Avatar asked Nov 10 '22 16:11

Freshblood


1 Answers

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.
like image 121
Dmitry Harnitski Avatar answered Nov 14 '22 23:11

Dmitry Harnitski