473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Session Cookie and HttpWebResponse

Hi,

I'm trying the following:
- Imitate a Logon using a Post with HttpWebRequest on remote Webserver (asp
3.0 page using https)
- On success redirect to the page (encapsuled in an iframe) supplied by the
remote Webserver

I can successfuly logon but when I redirect to the supplied url, the
webserver does not know me anymore an redirects me back to login page.. I
was told that I need to store the session cookie supplied by the remote
webserver on my webserver, but I don't know how to pass the session cookie
to my my webserver..

Sample Code follows:
Dim urlRed As String = https://book.bla.at/AM/booking/asp/login.asp
Dim wReq As HttpWebRequest = CType(WebRequest.Create(urlRed),
HttpWebRequest)
wReq.ContentType = "application/x-www-form-urlencoded"
wReq.Method = "POST"

Dim sPostData As String = "" & Session.SessionID
sPostData = sPostData & "&USERNAME=" & HttpUtility.UrlEncode("myuser")
sPostData = sPostData & "&password=" & HttpUtility.UrlEncode("mypassword")
sPostData = sPostData & "&language=" & HttpUtility.UrlEncode("DE_AT")

Dim enc As System.Text.Encoding = System.Text.Encoding.UTF8
Dim bPostBuffer As Byte() = enc.GetBytes(sPostData)
wReq.ContentLength = bPostBuffer.Length
Dim streamPostData As Stream = wReq.GetRequestStream()
streamPostData.Write(bPostBuffer, 0, bPostBuffer.Length)
streamPostData.Close()

Dim wResp As HttpWebResponse = CType(wReq.GetResponse(), HttpWebResponse)
Dim responseStream As StreamReader = New
StreamReader(wResp.GetResponseStream(), enc)

' Here I get the correct url to the welcome page encapsuled in html
Dim html As String = responseStream.ReadToEnd()wResp.Close()
responseStream.Close()

Response.Write(html)

Thanks and Regards
Karsten
Nov 17 '05 #1
3 7351
You can't really pass the session cookie, or any form of cookie. This is
because cookies have certain security on them which means that only the
domain/application that assigned them, can get them (this was at the centre
of a Passport hack a few years ago).
Instead, you can pass the variables in to a querystring on the request, or
you can use a stateserver/sql server to store the cookie data and have both
webservers get the information off that.

--
Philip Q
Microsoft MVP [ASP.NET]
http://aspalliance.com/wisemonk/

"Karsten Grombach" <ay*******@spamumluex.de> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
Hi,

I'm trying the following:
- Imitate a Logon using a Post with HttpWebRequest on remote Webserver (asp 3.0 page using https)
- On success redirect to the page (encapsuled in an iframe) supplied by the remote Webserver

I can successfuly logon but when I redirect to the supplied url, the
webserver does not know me anymore an redirects me back to login page.. I
was told that I need to store the session cookie supplied by the remote
webserver on my webserver, but I don't know how to pass the session cookie
to my my webserver..

Sample Code follows:
Dim urlRed As String = https://book.bla.at/AM/booking/asp/login.asp
Dim wReq As HttpWebRequest = CType(WebRequest.Create(urlRed),
HttpWebRequest)
wReq.ContentType = "application/x-www-form-urlencoded"
wReq.Method = "POST"

Dim sPostData As String = "" & Session.SessionID
sPostData = sPostData & "&USERNAME=" & HttpUtility.UrlEncode("myuser")
sPostData = sPostData & "&password=" & HttpUtility.UrlEncode("mypassword")
sPostData = sPostData & "&language=" & HttpUtility.UrlEncode("DE_AT")

Dim enc As System.Text.Encoding = System.Text.Encoding.UTF8
Dim bPostBuffer As Byte() = enc.GetBytes(sPostData)
wReq.ContentLength = bPostBuffer.Length
Dim streamPostData As Stream = wReq.GetRequestStream()
streamPostData.Write(bPostBuffer, 0, bPostBuffer.Length)
streamPostData.Close()

Dim wResp As HttpWebResponse = CType(wReq.GetResponse(), HttpWebResponse)
Dim responseStream As StreamReader = New
StreamReader(wResp.GetResponseStream(), enc)

' Here I get the correct url to the welcome page encapsuled in html
Dim html As String = responseStream.ReadToEnd()wResp.Close()
responseStream.Close()

Response.Write(html)

Thanks and Regards
Karsten


Nov 17 '05 #2
Hi Philip,

Thanks for your reply, but I don't quite understand you...
Using the sateserver/sqlserver is no option as we cannot change the legacy
asp application to use that anymore..
Do I have to pass my current (asp.net ) session ID with the
querystring/postdata to the asp application?
Regards
Karsten
"Philip Q [MVP]" <wi******@mvps.org> schrieb im Newsbeitrag
news:ee****************@TK2MSFTNGP10.phx.gbl...
You can't really pass the session cookie, or any form of cookie. This is
because cookies have certain security on them which means that only the
domain/application that assigned them, can get them (this was at the centre of a Passport hack a few years ago).
Instead, you can pass the variables in to a querystring on the request, or
you can use a stateserver/sql server to store the cookie data and have both webservers get the information off that.

--
Philip Q
Microsoft MVP [ASP.NET]
http://aspalliance.com/wisemonk/

"Karsten Grombach" <ay*******@spamumluex.de> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
Hi,

I'm trying the following:
- Imitate a Logon using a Post with HttpWebRequest on remote Webserver

(asp
3.0 page using https)
- On success redirect to the page (encapsuled in an iframe) supplied by

the
remote Webserver

I can successfuly logon but when I redirect to the supplied url, the
webserver does not know me anymore an redirects me back to login page.. I was told that I need to store the session cookie supplied by the remote
webserver on my webserver, but I don't know how to pass the session cookie to my my webserver..

Sample Code follows:
Dim urlRed As String = https://book.bla.at/AM/booking/asp/login.asp
Dim wReq As HttpWebRequest = CType(WebRequest.Create(urlRed),
HttpWebRequest)
wReq.ContentType = "application/x-www-form-urlencoded"
wReq.Method = "POST"

Dim sPostData As String = "" & Session.SessionID
sPostData = sPostData & "&USERNAME=" & HttpUtility.UrlEncode("myuser")
sPostData = sPostData & "&password=" & HttpUtility.UrlEncode("mypassword") sPostData = sPostData & "&language=" & HttpUtility.UrlEncode("DE_AT")

Dim enc As System.Text.Encoding = System.Text.Encoding.UTF8
Dim bPostBuffer As Byte() = enc.GetBytes(sPostData)
wReq.ContentLength = bPostBuffer.Length
Dim streamPostData As Stream = wReq.GetRequestStream()
streamPostData.Write(bPostBuffer, 0, bPostBuffer.Length)
streamPostData.Close()

Dim wResp As HttpWebResponse = CType(wReq.GetResponse(), HttpWebResponse) Dim responseStream As StreamReader = New
StreamReader(wResp.GetResponseStream(), enc)

' Here I get the correct url to the welcome page encapsuled in html
Dim html As String = responseStream.ReadToEnd()wResp.Close()
responseStream.Close()

Response.Write(html)

Thanks and Regards
Karsten

Nov 17 '05 #3
when using a proxy page, the solution is quite simple: just add hidden input
fields and submit them to the legacy server..
i don't really like the javascript part, but after having spent an afternoon
and a morning trying to solve this, i won't complain...

thanks all, for helping me!

here my proxpage which gets set to the iframe src attribute..

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="airmanagerproxy.aspx.vb" Inherits="TestWeb.airmanagerproxy"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>airmanagerproxy</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="GridLayout" >
<script language=javascript>
function submitIt()
{

document.proxyForm.action="https://book.bla.at/AM/business/AT/booking/asp/lo
gin.asp";
document.proxyForm.method="POST"

document.proxyForm.submit();

}
window.setTimeout("submitIt()",2000);

</script>
<form id="proxyForm" method="post" runat="server">
</form>
</body>
</html>

CodeBehind:
Imports System.Web.UI.HtmlControls
Public Class airmanagerproxy
Inherits System.Web.UI.Page
#Region " Vom Web Form Designer generierter Code "

'Dieser Aufruf ist für den Web Form-Designer erforderlich.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: Diese Methode ist für den Web Form-Designer erforderlich
'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
InitializeComponent()
End Sub
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Hier Benutzercode zur Seiteninitialisierung einfügen
Me.EnableViewState = False

' create the hiddenfields
' values are still hardcoded
Dim userName As HtmlInputHidden = New HtmlInputHidden()
userName.Name = "USERNAME"
userName.ID = "USERNAME"
userName.Value = "xxx"
Dim password As HtmlInputHidden = New HtmlInputHidden()
password.Name = "password"
password.ID = "password"
password.Value = "xxx"
Dim language As HtmlInputHidden = New HtmlInputHidden()
language.Name = "language"
language.ID = "language"
language.Value = "DE_AT"

' add the hiddenfields to the form
Dim form As Control = Me.FindControl("proxyForm")
form.Controls.Add(userName)
form.Controls.Add(password)
form.Controls.Add(language)

End Sub
End Class

"John Saunders" <jo***********@surfcontrol.com> schrieb im Newsbeitrag
news:%2***************@TK2MSFTNGP12.phx.gbl...
Sorry, I don't think you can do what you want to do in the way you want to
do it.

Work it out step by step. Let's call the client browser machine A, your
ASP.NET server machine B, and the legacy server machine C. A makes some
request to B. B logs into C and gets a cookie sent back to it by C.

BTW, I don't see you storing that cookie. Look into the CookieContainer
class.

So, B figures out where C redirected it to then puts that URL into the SRC
attribute of an IFRAME and sends it to A. A then requests that URL but does not send to C the cookie which C sent to B, which isn't surprising because B didn't store it, and certainly didn't send it to A, and if it had sent it to A, then A wouldn't know to send it to C, which A has never heard of!

It sounds like you're going to need some sort of proxy page running on B.
You'd have to store the cookie you get during login to C into Session state on B, then send your user an IFRAME pointing to your proxy on B, with the
real URL as a query parameter. The proxy would load up the CookieContainer
from Session state, request the legacy page (from the query parameter) via a WebRequest, and then return the output back to A.

You will similarly want to use a CookieContainer in that proxy page, and
store any new cookies in Session state for use in subsequent requests during the same session.

Note that this analysis totally ignores any issues with SSL, as it's too
early in the morning for that!

Also note that this might be a good job for an HttpHandler on B, as the
"proxy page" isn't really a page (it wouldn't have any controls on it).

Good Luck. THis sounds like a generally-useful piece of functionality you're going to implement. Let us know how it works out.
--
John Saunders
Internet Engineer
jo***********@surfcontrol.com
"Karsten Grombach" <ay*******@spamumluex.de> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
Hi,

I'm trying the following:
- Imitate a Logon using a Post with HttpWebRequest on remote Webserver

(asp
3.0 page using https)
- On success redirect to the page (encapsuled in an iframe) supplied by

the
remote Webserver

I can successfuly logon but when I redirect to the supplied url, the
webserver does not know me anymore an redirects me back to login page.. I was told that I need to store the session cookie supplied by the remote
webserver on my webserver, but I don't know how to pass the session cookie to my my webserver..

Sample Code follows:
Dim urlRed As String = https://book.bla.at/AM/booking/asp/login.asp
Dim wReq As HttpWebRequest = CType(WebRequest.Create(urlRed),
HttpWebRequest)
wReq.ContentType = "application/x-www-form-urlencoded"
wReq.Method = "POST"

Dim sPostData As String = "" & Session.SessionID
sPostData = sPostData & "&USERNAME=" & HttpUtility.UrlEncode("myuser")
sPostData = sPostData & "&password=" & HttpUtility.UrlEncode("mypassword") sPostData = sPostData & "&language=" & HttpUtility.UrlEncode("DE_AT")

Dim enc As System.Text.Encoding = System.Text.Encoding.UTF8
Dim bPostBuffer As Byte() = enc.GetBytes(sPostData)
wReq.ContentLength = bPostBuffer.Length
Dim streamPostData As Stream = wReq.GetRequestStream()
streamPostData.Write(bPostBuffer, 0, bPostBuffer.Length)
streamPostData.Close()

Dim wResp As HttpWebResponse = CType(wReq.GetResponse(), HttpWebResponse) Dim responseStream As StreamReader = New
StreamReader(wResp.GetResponseStream(), enc)

' Here I get the correct url to the welcome page encapsuled in html
Dim html As String = responseStream.ReadToEnd()wResp.Close()
responseStream.Close()

Response.Write(html)

Thanks and Regards
Karsten


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dax Westerman | last post by:
I have a site that I'm trying to migrate to ASP.NET from ASP, and the foremost stumbling block I'm hitting is session state between the ASP and ASP.NET applications. In order to access this...
1
by: ¼­°­È£ | last post by:
use HttpWebrequest. get cookie in URL1 and use this cookie in URL2 how to coding?
0
by: Peter Qian | last post by:
Hi, I'm working on a windows form based program that can log into a web service (Apache based, https is used for auth). I was able to post the login data and obtain a sessionID. However I'm not...
0
by: Dax Westerman | last post by:
I have a site that I'm trying to migrate to ASP.NET from ASP, and the foremost stumbling block I'm hitting is session state between the ASP and ASP.NET applications. In order to access this...
3
by: Amil | last post by:
Please don't repond to this if you are guessing or just don't know the answer. I'm trying to login to a backend system running Java/Tomcat. I create a HttpWebRequest with the login data and do...
4
by: Chris Newby | last post by:
My project currently requires that I integrate an ASP.NET application with an ASP application. One of the issues I'm having is that I have some very long strings being created in an ASP.NET...
0
by: Dave Alvarado | last post by:
I'm working on some code that gathers data from a web interface to one of our servers (classic ASP). The problem is that the site uses session variables to store login information, so to move from...
2
by: =?Utf-8?B?RGF2aWQgS2lya2xhbmQ=?= | last post by:
Hi, I am trying to connect to an https server and download a file. The server requires a client side certificate, which I have loaded successfully using the X509Certificate class. I then add this...
1
by: ALA | last post by:
Hi, does anybody know if it is possible to pass the SessionID with a web request by using a cookie so that the invoked page in the same domain can access the session objects of the current user?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.