SignalR .Net client fails to connect (upd: how to set auth. cookie?)

孤街醉人 提交于 2019-12-11 11:43:17

问题


This thing is dragging me nuts.

I have a .net 4.0 console app and I have an MVC web app.

javascript clients can connect and talk to the server - no problems here...

but my .net client throws System.AggregateException with InnerException = "Unexpected character encountered while parsing value: <. Path...

so I created an empty MVC3 app, added SignalR libraries, and .net client surprisingly connects to that. But for some reason it doesn't to the other one. I've checked everything, both MVC3 apps, both use the same SignalR libs, the same NewtonsoftJson... I thought it must be something with the routing, I guess no - js client works.

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

What could it be?

UPD: I have figured... it's because FormsAuthentication on the server. Now is there any way to feed .ASPXAUTH cookie to SignalR so it can connect to the server?


回答1:


The solution by Agzam was really helpful, but if anyone else uses the posted code it is critical that you close the HttpWebResponse before exiting GetAuthCookie. If you don't you will find that whenever you use SignalR to invoke a method on the server, the request (under most circumstances) will queue indefinitely on the client and will neither succeed nor fail.

Note. The original code worked in the test environment when everything was on my PC, but failed consistently when the website was hosted on a remote server.

here is the modified code I ended up using

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;
}

its a very minor change , but it will save you a lot of debugging time.




回答2:


Ok... stupid me... SignalR failed to connect because it cannot breach server's Forms authentication. So what needed to be done is to get the auth cookie and stick it to the HubConnection.CookieContainer...

so I wrote this method method to login with a username and get the 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;
}

And used it like this:

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

Works perfectly!




回答3:


Just use this for reading cookies:

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


来源:https://stackoverflow.com/questions/13022415/signalr-net-client-fails-to-connect-upd-how-to-set-auth-cookie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!