Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging user identity during an active SignalR connection — how to reconnect?
    text
    copied!<p>I'm having trouble with SignalR and users changing identity in an ASP.NET MVC 4 application. I'm using Forms authentication with a custom membership provider.</p> <p>The view that I have SignalR on also has a log in (and log out) button, and it is imperative that both logged in and logged out users are able to look at this view (and are all connected to the SignalR group associated with that view). However, I keep running into trouble when people log in. What's the recommended practice to 'reconnect' the underlying SignalR connection when the user identity changes? The view is one among several other views shown by other actions. This is the only one that has the SignalR javascript included.</p> <p>In fact, I'm not even all that interested in the identity of the user, so turning user affinity off somehow would be OK (but I couldn't find a way to do that). It would also be OK for a user change to trigger a page reload, as the login/logout took place in a window other than the one that's maintaining the connection, so the latter doesn't have the user's focus.</p> <p>The question, in other words: <strong>How do I keep an underlying SignalR connection going when the user identity changes from a logged in user to a user that's logged out, and vice versa?</strong></p> <h2>Things I have tried</h2> <ol> <li><p>Handling log in/log out events on-page, like this: <code>$("a[href='/Account/Logout']").click(function () { $.connection.hub.stop(); });</code></p> <p>This works, but this will require SignalR on every page where users are able to log in or out, not just the one that actually uses SignalR.</p></li> <li><p>Adding an additional group on the hub that is keyed with the ASP.NET Session ID, which stays consistent between logging in and out. Upon login or logout, I notified the client by sending the group with that session ID a trigger to reconnect. This just turned out to be too late, by the end of the request the identity had already been changed, so the exception fired.</p></li> </ol> <h2>More details about my setup</h2> <p>For this particular use case, the user is presented with a page that includes SignalR. I join the hub as per usual:</p> <pre><code>var hub = $.connection.theNameOfTheHub; $.connection.hub.start().done(function () { hub.server.joinList(id); }); </code></pre> <p>For the purposes of this example, I changed a few names. Basically, I want to monitor changes on a list of items displayed on the page. I join a group using the id of the list as an identifier.</p> <p>The exception message is "<code>System.InvalidOperationException</code>: Unrecognized user identity. The user identity cannot change during an active SignalR connection." and it comes from <code>Microsoft.AspNet.SignalR.PersistentConnection.GetConnectionId</code>. I've looked up the source and it compares the actual <code>context.Request.User.Identity.Name</code> to the one that was stored in the connection token. It all makes sense, but what I'm looking for is a workaround.</p> <p>I'm using <a href="http://nuget.org/packages/Microsoft.AspNet.SignalR">Microsoft.AspNet.SignalR 1.1.2</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload