473,397 Members | 1,961 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,397 software developers and data experts.

WebRequest and ASP username/password form authentication

Please help, I've scoured the web but cannot find an answer to this!

I want to use WebRequest to get the contents of a web page. The trouble is,
the page require you to go to a login page (using ASP - not ASP.NET), and
enter a username and password to get in.

How do I use WebRequest in this situation? I cannot find any way of
supplying the username and password. The '.Credentials' does not work because
that is only for Windows authentication, not forms authentication from the
web site.

Please help! At the moment I am forcing my user to go to the site and copy
and paste the text in. This is annoying because the content of the site
changes.
Nov 16 '05 #1
5 17552
Tony wrote:
Please help, I've scoured the web but cannot find an answer to this!

I want to use WebRequest to get the contents of a web page. The
trouble is, the page require you to go to a login page (using ASP -
not ASP.NET), and enter a username and password to get in.

How do I use WebRequest in this situation? I cannot find any way of
supplying the username and password. The '.Credentials' does not work
because that is only for Windows authentication, not forms
authentication from the web site.
You have to supply user name and password with a POST request yourself.
Please help! At the moment I am forcing my user to go to the site and
copy and paste the text in. This is annoying because the content of
the site changes.


First, you'll have to understand how the site actually works. Go to the
login page and check its HTML source. Look for the names of all input
controls related to login: user name, password, probably the login button as
well. This is the request data you want to send to login.

Next, look for the <form action="..."> tag that contains the login controls.
This is your HttpWebRequest's URI.

Assuming there's no client-side scripting or additional (hidden) fields,
that's all you need to create a HttpWebRequest instance for login.

You'll also need to understand how the web application implements session
tracking. You mentioned that it's an ASP application, so it's quite safe to
assume the web app is using cookies. Make sure to associate a
CookieContainer with all your HttpWebRequest objects in order to send the
session cookie with each subsequent request.

Cheers,
--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #2
Hi Joerg.

Thanks for the reply. However, I think the site uses the GET method, not the
POST method. I have tried manually putting the address of the right page into
Internet Explorer with the field names and values, but it does not work.

Is there a way to do what you have recommended using GET? Also, I am not
sure about cookies. Is there a good beginner's reference on programming
cookies in C#?

Thanks in advance.

Tony

"Joerg Jooss" wrote:
Tony wrote:
Please help, I've scoured the web but cannot find an answer to this!

I want to use WebRequest to get the contents of a web page. The
trouble is, the page require you to go to a login page (using ASP -
not ASP.NET), and enter a username and password to get in.

How do I use WebRequest in this situation? I cannot find any way of
supplying the username and password. The '.Credentials' does not work
because that is only for Windows authentication, not forms
authentication from the web site.


You have to supply user name and password with a POST request yourself.
Please help! At the moment I am forcing my user to go to the site and
copy and paste the text in. This is annoying because the content of
the site changes.


First, you'll have to understand how the site actually works. Go to the
login page and check its HTML source. Look for the names of all input
controls related to login: user name, password, probably the login button as
well. This is the request data you want to send to login.

Next, look for the <form action="..."> tag that contains the login controls.
This is your HttpWebRequest's URI.

Assuming there's no client-side scripting or additional (hidden) fields,
that's all you need to create a HttpWebRequest instance for login.

You'll also need to understand how the web application implements session
tracking. You mentioned that it's an ASP application, so it's quite safe to
assume the web app is using cookies. Make sure to associate a
CookieContainer with all your HttpWebRequest objects in order to send the
session cookie with each subsequent request.

Cheers,
--
Joerg Jooss
jo*********@gmx.net

Nov 16 '05 #3
Ho Joerg,

There is a hidden input field called Validate. Does this impact the code? Do
I need to account for it?

<form name="LOG01" action="/WebETC/employee.login.asp?" method="POST"
onSubmit="return validateLOG01(this)">
<input type="hidden" name="Validate" ="1">
<table border="0" cellspacing="0" cellpadding="2">
<tr>
<td align="right"><p class="formcaption">Agent Login ID</p></td>
<td>
<input type="text" name="username" class="formfield" maxlength="9" value="">

"Joerg Jooss" wrote:
Tony wrote:
Please help, I've scoured the web but cannot find an answer to this!

I want to use WebRequest to get the contents of a web page. The
trouble is, the page require you to go to a login page (using ASP -
not ASP.NET), and enter a username and password to get in.

How do I use WebRequest in this situation? I cannot find any way of
supplying the username and password. The '.Credentials' does not work
because that is only for Windows authentication, not forms
authentication from the web site.


You have to supply user name and password with a POST request yourself.
Please help! At the moment I am forcing my user to go to the site and
copy and paste the text in. This is annoying because the content of
the site changes.


First, you'll have to understand how the site actually works. Go to the
login page and check its HTML source. Look for the names of all input
controls related to login: user name, password, probably the login button as
well. This is the request data you want to send to login.

Next, look for the <form action="..."> tag that contains the login controls.
This is your HttpWebRequest's URI.

Assuming there's no client-side scripting or additional (hidden) fields,
that's all you need to create a HttpWebRequest instance for login.

You'll also need to understand how the web application implements session
tracking. You mentioned that it's an ASP application, so it's quite safe to
assume the web app is using cookies. Make sure to associate a
CookieContainer with all your HttpWebRequest objects in order to send the
session cookie with each subsequent request.

Cheers,
--
Joerg Jooss
jo*********@gmx.net

Nov 16 '05 #4
Tony wrote:
Hi Joerg.

Thanks for the reply. However, I think the site uses the GET method,
not the POST method.
That would be pretty stupid, as it exposes the password to the public --
it's appended to the URL. Note that unless the web application specifically
accepts or rejects GET or POST, both methods should work.
I have tried manually putting the address of the
right page into Internet Explorer with the field names and values,
but it does not work.
Yes, that's simple first test whether you've got all parameters right. Look
for hidden fields or JavsScript that is executed prior to form sumbmission.
Is there a way to do what you have recommended using GET?
Set your WebRequest's Method property to GET (it's the default anyway) and
append all required to the requested URI. You can use System.UriBuilder to
construct the URI with a properly encoded query string.
Also, I am
not sure about cookies. Is there a good beginner's reference on
programming cookies in C#?


There's actually little you have to do. Simply create a
System.Net.CookieContainer with your first request and reuse this instance
with all subsequent requests.

CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest firstRequest
= (HttpWebRequest) WebRequest.Create("http://host/path/to/login.asp");
firstRequest .CookieContainer = cookieContainer;

....

HttpWebRequest secondRequest
= (HttpWebRequest) WebRequest.Create("http://host/path/to/page.asp");
secondRequest .CookieContainer = cookieContainer;

As a client, all you need to do is make sure the server gets its cookies
back. The CookieContainer does all of the magic begind the scenes, you just
need to make sure it's available to your request.
Cheers,
--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #5
Tony wrote:
Ho Joerg,

There is a hidden input field called Validate. Does this impact the
code? Do I need to account for it?
Most likely.

<form name="LOG01" action="/WebETC/employee.login.asp?" method="POST"
onSubmit="return validateLOG01(this)">
<input type="hidden" name="Validate" ="1">

Check what the script function validateLOG01 does -- maybe it writes to this
or another hidden field.

Cheers,

--
Joerg Jooss
jo*********@gmx.net

Nov 16 '05 #6

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

Similar topics

17
by: James Johnson | last post by:
Dear C#dex, I define a variable: HttpWebRequest webRequest and run the following request webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest; The webRequest object returns values...
2
by: Tony | last post by:
Hi all, I need some help please using WebRequest with a page that requires you to log in through a different page. I have a roster that is located at our internal address of...
0
by: CodeMotion | last post by:
I have access to an internet directory and files will be placed in this directory periodically. I am writing a service to poll that directory and download the files for processing. I have figured...
0
by: IronYuppie | last post by:
Hi all-- I'm retrieving contents from my site using the retrieving images from the database method listed here: ...
1
by: Bruno | last post by:
Hello friends I'm developing a app o send xml data to a web server using HTTP POST.To do that Im using the WebRequest VB class. I can send the data sucessfully to the web server, using: Dim...
0
by: WIWA | last post by:
Hi, I want to login to a password protected website and fetch the content of the page behind. I have based my code on http://weblogs.asp.net/jdennany/archive/2005/04/23/403971.aspx. When I use...
0
by: tascien | last post by:
Hi guys, when i use winhttp, and the server returns status 500, I get the text that the server returned anyway... when i use webrequest, and the server returns status 500, webrequest just...
5
by: libra786 | last post by:
I have created a blog and have added a login box which prompts the user for login and id before posting- The username and password have been stored in the database, however when i enter the username...
1
by: S Moran | last post by:
c# vs2005 pro been asked to write a small app that "punches in" via a POST to a URL. if the response from the server contains the text "Punch Recorded" then all is well. Whats happening is that...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.