Newbie question: How to accomplish an HTTP Post in ASP.NET

  • Thread starter Thread starter shland
  • Start date Start date
S

shland

Hi, I'm hoping someone can point me in the right direction. I have a set of
..NET pages on which the user has to login. I also have some .asp pages that
I haven't converted yet. I want to be able to send my user to those .asp
pages without making them log in again (the .asp pages also check for
authorization and make the user log in if no credentials exist.) So, I'd
like to do a POST of the login information when the user clicks on a button
or whatever to go to those .asp pages.

I could put a standard HTML form on the page and allow it to do the post
when the user clicks on a submit button, but then I need to get the login
info from .NET variables to the HTML form input fields. Can I do this, and
is this the best way?

I also have experimented with using HTTPWebRequest object, but so far I
cannot get the browser to stay on the posted URL page (it seems to go the
the URL and then return right back to my .aspx page where I originated the
request). This doesn't happen when I use the HTML form method. Can I
emulate the behavior of the HTML form POST using the HTTPWebRequest object,
or some other variation? If so, where am I going wrong? I'd prefer to
accomplish the job using something of this sort, rather than using an HTML
form, which seems hokey. Here's my experimental code (values are hardcoded
for the purposes of testing): (again I'm a newbie, so be gentle and talk to
me in easy to understand language ;-)


private void btnTest_Click(object sender, System.EventArgs e)

{

string strId = "Admin";

string strPassword = "testpassword";

ASCIIEncoding encoding=new ASCIIEncoding();

string postData="Username="+strId +"&Password="+strPassword;

byte[] data = encoding.GetBytes(postData);

HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/test/test.asp");

myRequest.Method = "POST";

myRequest.ContentType="application/x-www-form-urlencoded";

myRequest.ContentLength = data.Length;

Stream newStream=myRequest.GetRequestStream();

newStream.Write(data,0,data.Length);

newStream.Close();

}


Thanks in advance for helping a newbie!
Sheryl
 
Hi Sheryl,

We need to back up a bit. You say that you have .asp pages that check for
authorization. Prior to migrating to ASP.Net, you must have had some type of
method for storing the login information so that your users didn't have to
log in to get to every page. What was that method?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
Hi,

Although I prefer using the Form method you are missing one line that
initiate the call to the server:

Dim myHttpWebResponse As HttpWebResponse =
CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

Natty Gur[MVP]
Phone Numbers:
Office: +972-(0)9-7740261
Fax: +972-(0)9-7740261
Mobile: +972-(0)58-888377
 
Hi Kevin - the asp pages test for session vars that indicate the user was
altready authenticated. In the absense of this, they look for
authentication information passed from a login form via POST that can then
be checked against a database.

S.

Kevin Spencer said:
Hi Sheryl,

We need to back up a bit. You say that you have .asp pages that check for
authorization. Prior to migrating to ASP.Net, you must have had some type of
method for storing the login information so that your users didn't have to
log in to get to every page. What was that method?

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.

shland said:
Hi, I'm hoping someone can point me in the right direction. I have a
set
of
.NET pages on which the user has to login. I also have some .asp pages that
I haven't converted yet. I want to be able to send my user to those ..asp
pages without making them log in again (the .asp pages also check for
authorization and make the user log in if no credentials exist.) So, I'd
like to do a POST of the login information when the user clicks on a button
or whatever to go to those .asp pages.

I could put a standard HTML form on the page and allow it to do the post
when the user clicks on a submit button, but then I need to get the login
info from .NET variables to the HTML form input fields. Can I do this, and
is this the best way?

I also have experimented with using HTTPWebRequest object, but so far I
cannot get the browser to stay on the posted URL page (it seems to go the
the URL and then return right back to my .aspx page where I originated the
request). This doesn't happen when I use the HTML form method. Can I
emulate the behavior of the HTML form POST using the HTTPWebRequest object,
or some other variation? If so, where am I going wrong? I'd prefer to
accomplish the job using something of this sort, rather than using an HTML
form, which seems hokey. Here's my experimental code (values are hardcoded
for the purposes of testing): (again I'm a newbie, so be gentle and
talk
to
me in easy to understand language ;-)


private void btnTest_Click(object sender, System.EventArgs e)

{

string strId = "Admin";

string strPassword = "testpassword";

ASCIIEncoding encoding=new ASCIIEncoding();

string postData="Username="+strId +"&Password="+strPassword;

byte[] data = encoding.GetBytes(postData);

HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/test/test.asp");

myRequest.Method = "POST";

myRequest.ContentType="application/x-www-form-urlencoded";

myRequest.ContentLength = data.Length;

Stream newStream=myRequest.GetRequestStream();

newStream.Write(data,0,data.Length);

newStream.Close();

}


Thanks in advance for helping a newbie!
Sheryl
 
Thanks for the hint... I get a 401 unauthorized error when I include this
line... the test target page test.asp does not have any security checks in
it, just a couple of response.write's, and it is not in the same directory
or app as the originating code, so it shouldn't be affected by the asp.net
security or any other security check. Is there something else I'm missing?

Sheryl
 
Back
Top