2012-10-23 69 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"];