webclient object and forms authentication

Discussion in 'ASP .Net' started by buran, Sep 13, 2003.

  1. buran

    buran Guest

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


    buran, Sep 13, 2003
    1. Advertisements

  2. You have to log in, same as a user would.
    John Saunders, Sep 13, 2003
    1. Advertisements

  3. buran

    buran Guest

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

    Thakn you
    buran, Sep 15, 2003
  4. 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.
    John Saunders, Sep 15, 2003
  5. buran


    Oct 7, 2006
    Likes Received:
    login problem in webclient


    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
    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...
    pcaddict, Oct 7, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.