我将SignalR 1与带有表单身份验证的MVC4 C#Web应用程序一起使用。
我的JavaScript布局页面中有一个代码:
$(documnet).ready(function(){
connect to hub code ...
})
我要断开用户与集线器的连接,并在他登录并确认确定后再次开始连接。
我想从我的帐户控制器和方法中的服务器端进行操作:
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
}
我要这样做的原因是,如果用户在登录后更改为身份验证并且连接仍然存在,则SignalR会引发错误。错误是:
连接ID的格式错误。
最佳答案
您无法从服务器停止和启动SignalR连接。您需要致电
$.connection.hub.stop(); //on the client before the user attempts to log on and then call
$.connection.hub.start(); //after the log on attempt has completed.