2012-10-23 63 views
1

这件事情正在拖延我坚果。SignalR .Net客户端无法连接(更新:如何设置身份验证cookie?)

我有一个.net 4.0控制台应用程序,我有一个MVC网络应用程序。

的JavaScript客户端可以连接,跟我们的服务器 - 在这里没有任何问题......

但我的.NET客户端抛出System.AggregateException用在分析价值时遇到的InnerException =“意外的字符:<路径...。

所以我创建了一个空的MVC3应用程序,添加了SignalR库,而.net客户端却令人惊讶地连接到了这个,但由于某种原因,它没有与另一个。相同的SignalR库,相同的NewtonsoftJson ...我认为它必须是路由的东西,我猜没有 - js客户端的工作原理

var connection = new HubConnection("http://localhost:58746"); 
var hubProxy = connection.CreateProxy("myProxy"); 
connection.Start().Wait() // it fails here on Wait 

它可能是什么?

UPD:我想通过...这是因为服务器上的FormsAuthentication。现在有没有办法将.ASPXAUTH cookie提供给SignalR,以便它可以连接到服务器?

+0

你为什么要这么做等待? – MaYaN

+0

你有没有试过看小提琴看看http反应是什么?或者,您可以使用GetError扩展方法来获取底层http响应。 GetError挂起异常。 – davidfowl

+0

@MaYaN你是什么意思,为什么?因为它在SignalR示例wiki页面上是这么说的...无论如何,它可以与我的其他应用程序协同工作,并且与我不得不使用的那个应用程序不兼容:( – Agzam

回答

3

好的......愚蠢的我...... SignalR无法连接,因为它无法破坏服务器的表单身份验证。那么,什么需要做的是获得身份验证cookie,并坚持到了HubConnection.CookieContainer ...

所以我写了这个方法,方法使用用户名登录并获得该cookie:

private Cookie GetAuthCookie(string user, string pass) 
{ 
    var http = WebRequest.Create(_baseUrl+"Users/Login") as HttpWebRequest; 
    http.AllowAutoRedirect = false; 
    http.Method = "POST"; 
    http.ContentType = "application/x-www-form-urlencoded"; 
    http.CookieContainer = new CookieContainer(); 
    var postData = "UserName=" + user + "&Password=" + pass + "&RememberMe=true&RememberMe=false&ReturnUrl=www.google.com"; 
    byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(postData); 
    http.ContentLength = dataBytes.Length; 
    using (var postStream = http.GetRequestStream()) 
    { 
     postStream.Write(dataBytes, 0, dataBytes.Length); 
    } 
    var httpResponse = http.GetResponse() as HttpWebResponse; 
    var cookie = httpResponse.Cookies[FormsAuthentication.FormsCookieName]; 
    httpResponse.Close(); 
    return cookie; 
} 

并用它是这样的:

var connection = new HubConnection(_baseUrl) 
       { 
        CookieContainer = new CookieContainer() 
       }; 
       connection.CookieContainer.Add(GetAuthCookie(_user, _pass)); 

完美的作品!

3

Agzam的解决方案确实很有帮助,但如果其他人使用发布的代码,在退出GetAuthCookie之前关闭HttpWebResponse是至关重要的。如果你不这样做,你会发现无论何时使用SignalR调用服务器上的方法,请求(在大多数情况下)将无限期地排队在客户端上,并且既不会成功也不会失败。

注意。当所有东西都在我的电脑上时,原始代码在测试环境中工作,但当网站托管在远程服务器上时始终失败。

这里是修改后的代码,我结束了使用

private Cookie GetAuthCookie(string user, string pass) 
{ 
    var http = WebRequest.Create(_baseUrl+"Users/Login") as HttpWebRequest; 
    http.AllowAutoRedirect = false; 
    http.Method = "POST"; 
    http.ContentType = "application/x-www-form-urlencoded"; 
    http.CookieContainer = new CookieContainer(); 
    var postData = "UserName=" + user + "&Password=" + pass + "&RememberMe=true&RememberMe=false&ReturnUrl=www.google.com"; 
    byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(postData); 
    http.ContentLength = dataBytes.Length; 
    using (var postStream = http.GetRequestStream()) 
    { 
     postStream.Write(dataBytes, 0, dataBytes.Length); 
    } 
    var httpResponse = http.GetResponse() as HttpWebResponse; 
    var cookie = httpResponse.Cookies[FormsAuthentication.FormsCookieName]; 
    httpResponse.Close(); 
    return cookie; 
} 

它的一个非常微小的变化,但它会为你节省大量的调试时间。

0

就用这个读取Cookie:

var cookie = response.Cookies[".AspNet.ApplicationCookie"];