SignalR .Net客户端将无法连接(UPD:如何设置身份验证cookie的?) [英] SignalR .Net client fails to connect (upd: how to set auth. cookie?)

查看:194
本文介绍了SignalR .Net客户端将无法连接(UPD:如何设置身份验证cookie的?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这件事被拽我坚果。

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

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

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

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

但我的.NET客户端抛出System.AggregateException用的InnerException =意外的字符时遇到了分析值:其中;.路径...

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

所以我创建了一个空的MVC3应用程序,添加SignalR库和.NET客户端令人惊讶地连接到这一点。但由于某种原因它不给另一个。我检查了一切,无论MVC3应用程序,都使用相同的SignalR库,同样的NewtonsoftJson ...我想那一定是一些与路由,我想没有 - JS客户端工作

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:我已经想通......这是因为FormsAuthentication在服务器上。现在有没有办法养活.ASPXAUTH cookie来SignalR,因此它可以连接到服务器?

推荐答案

好吧...愚蠢的我...... SignalR连接失败,因为它无法突破服务器的Forms身份验证。那么,什么需要做的是,以获得身份验证cookie,并坚持下去的 HubConnection.CookieContainer ...

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...

所以我写了这个方法的方法登录用户名和获得的cookie:

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

和使用它是这样的:

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

完美的作品!

Works perfectly!

这篇关于SignalR .Net客户端将无法连接(UPD:如何设置身份验证cookie的?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆