webclient object and forms authentication

B

buran

How do I download file from the web when the site uses forms authentication?
Without the authentication, webclient object works.

Thanks,

Buran
 
J

John Saunders

buran said:
How do I download file from the web when the site uses forms authentication?
Without the authentication, webclient object works.

You have to log in, same as a user would.
 
B

buran

How can I login using WebClient or WebRequest object? Could you please give
sample code?

Thakn you
 
J

John Saunders

buran said:
How can I login using WebClient or WebRequest object? Could you please give
sample code?

Not really. The only code I have which does this is embedded in a complex
asynchronous program.

The idea is that you need to POST the username and password just like the
browser does. You will find that a downside is that the browser sends
name/value pairs, where the name is the name of the control to which the
value applies, like username=john. An issue is that in ASP.NET, the name is
likely to be something compound, like _ctl0:_ctl1:username. The numbering of
the _ctln names is likely to change over time as the form is maintained, so
your code will either have to change when the login page changes, or else
try to guess what the names are by looking at the form.

FYI, the way a login sequence usually works in Forms Authentication is:

1. Client requests: GET desiredPage.aspx
2. Server responds with a "302 Page moved" response, giving the location of
the login page:
3. Client requests the login page with a GET
4. Server responds with the login page, which includes the form fields
you'll need to fill in
5. Client sends the username/password with a POST to the login page
6. Server responds with a "302 Page moved" response, giving the location of
desiredPage.aspx and also a login cookie
7. Client requests desiredPage.aspx with a GET, including the login cookie,
and this time it succeeds

I hope this helps, and I'm sorry my code wouldn't help you much.
 
Joined
Oct 7, 2006
Messages
2
Reaction score
0
login problem in webclient

hi,

I am new to this forum...
I like programming a lot so my profession.

But this problem is driving me crazy..
i am trying to download a file from a website ( a text file ).
the URL like
https://someone.com/foldername/..
it lists some files in links...link to parent directory etc.

It also popups a login window and prompts for username and password..

I am using the following code.. in C#

webclient wb = new webclient();
NetworkCredential cred = new NetworkCredential(username, password, "");
wb.credentials = cred;
wb.downloadfile(url, destinationfilename)

But, this throws error 401 unauthorized.....

Someone pls help...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top