asp.net-mvc-4 - 断开客户端与服务器端信号器的连接

标签 asp.net-mvc-4 signalr

我将 SignalR 1 与带有表单例份验证的 MVC4 C# web 应用程序一起使用。
我的布局页面中有一段 JavaScript 代码:

$(documnet).ready(function(){
       connect to hub code ...
})

我想从集线器断开用户的连接,并在他登录并验证确定后再次开始连接。
我想从我的帐户 Controller 和方法中的服务器端执行此操作:
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 会抛出错误。错误是:

The connection id is in the incorrect format.

最佳答案

您无法从服务器停止和启动 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.

关于asp.net-mvc-4 - 断开客户端与服务器端信号器的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15889367/

相关文章:

asp.net-mvc-4 - MVC4 自定义不显眼的验证器不起作用

javascript - 如何调用这个JS函数并传递参数?

c# - 自定义ActionResult中可以使用void async方法吗?

c# - Windows 服务上的 SignalR 自托管

signalr - 如何仅向 SignalR 中的调用方客户端发送消息?

asp.net-mvc - 在 Url.action javascript 中给出动态操作名称

c# - 我的 mvc 应用程序中出现防伪错误

c# - MVC4 WebAPI 过滤器 : any way to stub Filter. 顺序?

javascript - 它使用 ASP.NET MVC 和 javascript 在 TreeView 中显示 "undefined"

jquery - 如何开发类似堆栈溢出的通知系统