如何在Web客户端上复制此cookie功能?

时间:2012-05-01 15:34:56

标签: jquery post cookies cross-domain

根据我的阅读,不可能跨域发送cookie,(据我所知,浏览器出于隐私原因阻止它们)。但是我希望有人可以告诉我有关解决方案的问题。

我已经在我们的.Net winForms客户端实现了这一点,但是我无法让它在我们的网络软件上运行。

场景:我有我的网站,这需要调用第三方系统,该系统使用存在于客户防火墙内的XML的休息实现,并且无法从其办公室外部访问(VPN也不是一种选择)

我设置了我的cookie:

$.cookie('name', 'value', {path : '/', domain : '192.168.254.164'});

并发帖请求

$.post(call, function(d) {}, 'text')
.success(function(d) {... /*do something*/

但是,cookie未发送(请参阅下面的请求和响应标题)

Request Headers
Accept    text/plain, */*; q=0.01
Accept-Encoding   gzip, deflate
Accept-Language   en-gb,en;q=0.5
Connection    keep-alive
Host  192.168.254.164:8080
Origin    http://localhost:27249
Referer   http://localhost:27249/index.html
User-Agent    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0


Response Headers
Access-Control-Allow-Orig...  http://localhost:27249
Cache-Control no-cache
Content-Type  application/xml;charset=UTF-8
Date  Tue, 01 May 2012 15:14:58 GMT
Server    Sun Java System Application Server 9.1_01
Set-Cookie    JSESSIONID=8f7fbcd40348c0b5d912830bca8a; Path=/App
Transfer-Encoding chunked
X-Powered-By  Servlet/2.5

我收到的回复告诉我我未经授权(这是因为没有设置cookie)。

我无法修改第三方服务器上的任何内容,因此在代理服务器上我无法使用JSONP,因为所有内容都是XML格式。

对于调试我尝试在发送之前读取cookie,但结果为'null':

alert($.cookie(_svcMnuCookieSessionIdKey));

我是网络开发人员的新手,所以这可能是一个奇怪的问题 - 但是你可以从响应标题中看到我相信我收到了一个cookie(我在登录时收到了相同的cookie) - 作为浏览器处理cookies,不应该保存并自动应用于我的请求吗?而不是我必须像上面那样手动添加它?尽管如此,JSESSIONID看起来在两个请求中都有不同的值,并且规范说我必须使用我在登录时获得的原始JSESSIONID作为我的cookie的值。

在我的.Net应用程序中,我这样做:

 Dim httpWebRequest As Net.HttpWebRequest = CType(Net.WebRequest.Create("http://192.168.254.164:8080/App/RestfulService"), Net.HttpWebRequest)

 httpWebRequest.UserAgent = My.Application.Info.AssemblyName
 httpWebRequest.KeepAlive = True 'set the connection keep-alive (this is the default)
 httpWebRequest.Headers.Set(Net.HttpRequestHeader.Pragma, "no-cache") 'we don't want caching to take place so we need to set the pragma header to say we don't want caching
 httpWebRequest.Method = "POST"

 If Not String.IsNullOrEmpty(_sessionId) Then 'it isn't used on some request, so don't add it if it is nothing
     Dim cookie As New System.Net.Cookie("JSESSIONID", _sessionId)
     cookie.Domain = New Uri("http://192.168.254.164:8080/App/RestfulService").Host
     httpWebRequest.CookieContainer = New Net.CookieContainer
     httpWebRequest.CookieContainer.Add(cookie)
 End If

 httpWebRequest.ContentType = "application/x-www-form-urlencoded"

 Dim postData As Byte() = New System.Text.UTF8Encoding(False).GetBytes(RichTextBox1.Text)

 httpWebRequest.ContentLength = postData.Length

 Using tmpStream As IO.Stream = httpWebRequest.GetRequestStream
     tmpStream.Write(postData, 0, postData.Length)
 End Using

 Dim httpWebResponse As Net.HttpWebResponse = CType(httpWebRequest.GetResponse, Net.HttpWebResponse)

 If WebValidation.WebResponseHasContent(httpWebResponse) Then

     Dim str As String = String.Empty
     'Read the raw HTML from the request
     Using sr As New IO.StreamReader(httpWebResponse.GetResponseStream, System.Text.Encoding.UTF8)
         str = sr.ReadToEnd
     End Using

 End If

Return str

那么,这是一个跨域cookie请求吗?以及如何在我的Web客户端中重新创建此功能?

2 个答案:

答案 0 :(得分:1)

我认为您没有尝试设置跨域Cookie 。系统不会要求你这样做。通常在这种情况下,您需要与第三方系统进行一些握手以获取令牌,或者在您的情况下首先(从系统)获取JSESSIONID。但是从你看来,你试图在第三方系统上设置cookie,而不是从系统接受cookie。那有意义吗?所以我期望你需要通过第三方系统发出请求以获取令牌,然后在尝试获取xml时为每个后续请求传回该令牌。

我认为在您的方案中,您拥有自己的网络应用和第三方应用。您可能会感到困惑的是,在您的浏览器上设置了Cookie,而在您的网络应用中设置了Cookie(用于身份验证)。

当您使用第三方系统进行身份验证并获得JSESSIONID后,请尝试将后续请求标头设置为包含“cookie”标头,其中包含JSESSIONID或第三方系统需要进行身份验证的任何内容。

请记住,在这种情况下,您通过网络应用程序将第三方作为服务进行呼叫,这是您的网络应用程序,即该第三方呼叫的“浏览器”。因此,当它对第三方系统进行后续调用时,它必须通过伪装一个cookie(包含您的身份验证会话ID)来模仿浏览器 - 您可以通过设置'cookie'请求标头来实现此目的。

答案 1 :(得分:-2)

你做不到。浏览器制造商花了很多时间和时间努力确保你想要完成的事情不会发生,即使你确实找到了一个临时的解决方案,它也会被认为是一个安全漏洞。可能会在下一个版本中修补。

相反,尝试弄清楚如何将所需信息传递到Web服务中。