locked
Context.Identity returns null on OnDisconnected Event RRS feed

  • Question

  • User246158972 posted

    Hi,

    I am getting an object reference error when a client disconnect from the hub, here's my code

    public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
            {
                string userName = Authorization.GetLogin(HttpContext.Current.User.Identity);
                string connectionId = Context.ConnectionId;
    
                User user;
                Users.TryGetValue(userName, out user);
    
                if (user != null)
                {
                    lock (user.ConnectionIds)
                    {
                        user.ConnectionIds.RemoveWhere(x => x.Equals(connectionId));
                        if(!user.ConnectionIds.Any())
                        {
                            User removedUser;
                            Users.TryRemove(userName, out removedUser);
                        }
                    }
                }
    
                return base.OnDisconnected(stopCalled);
            }

    the Context.Identity returns null and cannot be read. I've read that this is a bug in 2.0, but I am using 2.2

    Many Thanks!

    Friday, May 13, 2016 3:47 AM

Answers

  • User61956409 posted

    Hi recca,

    the Context.Identity returns null and cannot be read

    string userName = Authorization.GetLogin(HttpContext.Current.User.Identity);

    Do you mean that HttpContext.Current.User.Identity will return null in above code snippet? You could try to use HubCallerContext object to get User information.

    http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#contextproperty

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 14, 2016 7:12 AM
  • User61956409 posted

    Hi recca,

    As I mentioned in my previous reply, do you debug the code to check the value of HttpContext.Current.User.Identity? is it null? And you could try to use the following code to retrieve user name.

    string userName = Context.User.Identity.Name;

    Besides, according to your description, it seems that you’d like to retain information about users and their connections, if that is the case, you could refer to this link.

    http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections#inmemory

    Best Regards,

    Fei Han

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 23, 2016 9:37 AM

All replies

  • User61956409 posted

    Hi recca,

    the Context.Identity returns null and cannot be read

    string userName = Authorization.GetLogin(HttpContext.Current.User.Identity);

    Do you mean that HttpContext.Current.User.Identity will return null in above code snippet? You could try to use HubCallerContext object to get User information.

    http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#contextproperty

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 14, 2016 7:12 AM
  • User246158972 posted

    Hi Fei Han,

    What I want is to remove the user's ConnectionId from my dictionary on OnDisconnected event. But before stepping into this code snippet 

    if (user != null)
                {
                    lock (user.ConnectionIds)
                    {
                        user.ConnectionIds.RemoveWhere(x => x.Equals(connectionId));
                        if(!user.ConnectionIds.Any())
                        {
                            User removedUser;
                            Users.TryRemove(userName, out removedUser);
                        }
                    }
                }
    

    The app throws error in line 

    string userName = Authorization.GetLogin(HttpContext.Current.User.Identity);

    which throws the above error. And I am not able to remove the user or the user's connectionId from the dictionary.

    Wednesday, May 18, 2016 8:20 AM
  • User61956409 posted

    Hi recca,

    As I mentioned in my previous reply, do you debug the code to check the value of HttpContext.Current.User.Identity? is it null? And you could try to use the following code to retrieve user name.

    string userName = Context.User.Identity.Name;

    Besides, according to your description, it seems that you’d like to retain information about users and their connections, if that is the case, you could refer to this link.

    http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections#inmemory

    Best Regards,

    Fei Han

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 23, 2016 9:37 AM