I'm using SignalR 1 with MVC4 C# web application with form authentication. I have a code in my layout page in JavaScript :
$(documnet).ready(function(){
connect to hub code ...
})
I want to disconnect a user form the hub and start connect again after he does a login and validate ok. I want to do it from server side inside my account controller and method :
public ActionResult LogOn(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (System.Web.Security.Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
....here , disconnect from hub
....to make the user reconnect
}
The reason I want to do it is because SignalR throws an error if user changed to authenticated after login and the connection remains . The error is:
The connection id is in the incorrect format.
One way you could do what you ask is to write a disconnect event on your client that the server can call through SignalR. Maybe something somewhat like this:
Then, on the server, something like this:
If someone is still looking for solution(SignalR version 2.4.1):
Copy and paste the following function into your Hub
to force the client to disconnect in your hub
Try this:
Assuming your connection handle is
connection
. The challenge is accessing a handle to yourconnection
object in your Action Method.Try controlling everything from javascript. The following is a logout example, login would be similar.
From http://www.asp.net/signalr/overview/security/introduction-to-security:
You cannot stop and start SignalR connections from the server. You will need to call