5

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();
    }
  }
Freshblood
  • 6,285
  • 10
  • 59
  • 96

3 Answers3

3

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.
Dmitry Harnitski
  • 5,838
  • 1
  • 28
  • 43
0

This authentication approach will not cause Context.User.Identity.Name null value on SignalR 2.0 (https://github.com/tugberkugurlu/SignalRSamples/tree/master/ConnectionMappingSample):

   [HttpPost]
    [ActionName("Login")]
    public ActionResult PostLogin(LoginModel loginModel) {

        if (ModelState.IsValid) {

            FormsAuthentication.SetAuthCookie(loginModel.Name, true);
            return RedirectToAction("index", "home");
        }

        return View(loginModel);
    }

    [HttpPost]
    [ActionName("SignOut")]
    public ActionResult PostSignOut() {

        FormsAuthentication.SignOut();
        return RedirectToAction("index", "home");
    }

However, I did encounter Context.User null value when using MVC5 Identity model.

totoro
  • 195
  • 1
  • 9
-2

I used Context.ConnectionId for my SignalR 2.0 project and I'm very satisfied with it.

Storing connid for a static string in this case, or a static List to store incoming connection id's, because http is stateless, each postback will delete the information.

public class FeedHub : Hub

{   
    static string username = ""; 

    public override Task OnConnected() //event to fire whenever someone joins
    {
    var name = Context.ConnectionId;//capture the unique incoming connection id
    var username = name;//write it into the static string
    return base.OnConnected();
    }

    public override Task OnDisconnected() //event to fire whenever someone quits
    {
    //In here Context.User property is sometimes null because you only recognize authorized users.
    return base.OnDisconnected();
    }
}
  • Note that using static fields, and especially lists, to store server-side state is an absolutely terrible suggestion and nobody should ever, ever do it. Database, Cache, Files, all sorts, but not that. – Mark Rendle Apr 29 '14 at 16:24
  • Yea that's just a terrible idea – 3dd Aug 26 '14 at 08:59