1

这件事让我抓狂。

我有一个 .net 4.0 控制台应用程序和一个 MVC Web 应用程序。

javascript 客户端可以连接并与服务器对话 - 这里没有问题...

但我的 .net 客户端抛出 System.AggregateException with InnerException = "解析值时遇到意外字符:<.Path...

所以我创建了一个空的 MVC3 应用程序,添加了 SignalR 库,.net 客户端令人惊讶地连接到它。但由于某种原因,它对另一个没有。我已经检查了所有内容,两个 MVC3 应用程序,都使用相同的 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,以便它可以连接到服务器?

4

3 回答 3

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

完美运行!

于 2012-10-24T15:48:07.660 回答
3

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

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.

于 2012-12-21T20:49:54.123 回答
0

Just use this for reading cookies:

var cookie = response.Cookies[".AspNet.ApplicationCookie"];
于 2014-11-11T08:08:37.203 回答