HttpWebRequest Get

Discussion in 'ASP .Net' started by Jeremy Chapman, Nov 26, 2003.

  1. In IIS, I've got an application directory called AuthenticationTest. I've
    also got an app directory called TestWinAuth.
    Both are under the wwwroot directory.

    My AuthenticationTest app enables anonymous access, while the TestWinAuth
    app uses windows authentication. I have 1 file in my TestWinAuth directory
    called test.html

    In my AuthenticationTest app, I have the following code which does an HTTP
    Get on the file in the TestWinAuth directory. I thought that this would
    issue a challange response and attempt to authenticate me using windows
    authentication, but I just get an error 401 message. If I go to the link
    directly though I get authenticated. Is there any way to get the
    authentication to work?

    try
    {
    Label2.Text = http://MyServerName/TestWinAuth/test.html";
    HttpWebRequest req = (HttpWebRequest) WebRequest.Create(Label2.Text);

    // Define the request access method.
    req.Method = "GET";

    HttpWebResponse result = (HttpWebResponse) req.GetResponse();
    Label1.Text = "passed";
    }
    catch (WebException we)
    {
    // Display any errors. In particular, display any protocol-related
    error.
    if (we.Status == WebExceptionStatus.ProtocolError)
    {
    HttpWebResponse hresp = (HttpWebResponse) we.Response;
    Label1.Text = "Authentication Failed, " + hresp.StatusCode + "\r\n"
    +
    "Status Code: " + (int) hresp.StatusCode + "\r\n" +
    "Status Description: " + hresp.StatusDescription;
    }
    }
    catch (Exception ex)
    {
    Label1.Text = ex.Message;
    }
    Jeremy Chapman, Nov 26, 2003
    #1
    1. Advertising

  2. When you use your browser you are being identified - possibly via something
    called passthrough authentication (you didn't say if you get challenged).
    Anyway, when you make a call from the web classes you have to tell it who to
    make the call as as explained here

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfsystemnethttpwebrequestclasscredentialstopic.asp

    --
    Regards

    John Timney (Microsoft ASP.NET MVP)
    ----------------------------------------------
    <shameless_author_plug>
    Professional .NET for Java Developers with C#
    ISBN:1-861007-91-4
    Professional Windows Forms
    ISBN: 1861005547
    Professional JSP 2nd Edition
    ISBN: 1861004958
    Professional JSP
    ISBN: 1861003625
    Beginning JSP Web Development
    ISBN: 1861002092
    </shameless_author_plug>
    ----------------------------------------------

    "Jeremy Chapman" <> wrote in message
    news:...
    > In IIS, I've got an application directory called AuthenticationTest. I've
    > also got an app directory called TestWinAuth.
    > Both are under the wwwroot directory.
    >
    > My AuthenticationTest app enables anonymous access, while the TestWinAuth
    > app uses windows authentication. I have 1 file in my TestWinAuth

    directory
    > called test.html
    >
    > In my AuthenticationTest app, I have the following code which does an HTTP
    > Get on the file in the TestWinAuth directory. I thought that this would
    > issue a challange response and attempt to authenticate me using windows
    > authentication, but I just get an error 401 message. If I go to the link
    > directly though I get authenticated. Is there any way to get the
    > authentication to work?
    >
    > try
    > {
    > Label2.Text = http://MyServerName/TestWinAuth/test.html";
    > HttpWebRequest req = (HttpWebRequest) WebRequest.Create(Label2.Text);
    >
    > // Define the request access method.
    > req.Method = "GET";
    >
    > HttpWebResponse result = (HttpWebResponse) req.GetResponse();
    > Label1.Text = "passed";
    > }
    > catch (WebException we)
    > {
    > // Display any errors. In particular, display any protocol-related
    > error.
    > if (we.Status == WebExceptionStatus.ProtocolError)
    > {
    > HttpWebResponse hresp = (HttpWebResponse) we.Response;
    > Label1.Text = "Authentication Failed, " + hresp.StatusCode +

    "\r\n"
    > +
    > "Status Code: " + (int) hresp.StatusCode + "\r\n" +
    > "Status Description: " + hresp.StatusDescription;
    > }
    > }
    > catch (Exception ex)
    > {
    > Label1.Text = ex.Message;
    > }
    >
    >
    John Timney \(Microsoft MVP\), Nov 26, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Satinderpal Singh

    HttpWebResponse/HttpWebRequest problem...

    Satinderpal Singh, Jun 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,808
    Satinderpal Singh
    Jun 4, 2004
  2. Greg Reevosh via .NET 247

    Issue with System.Net.HttpWebRequest

    Greg Reevosh via .NET 247, Oct 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,384
    Feroze [msft]
    Oct 22, 2004
  3. Greg Reevosh via .NET 247

    Issue with System.Net.HttpWebRequest

    Greg Reevosh via .NET 247, Oct 31, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    449
    Greg Reevosh via .NET 247
    Oct 31, 2004
  4. Sojwal

    HttpWebRequest problem: Please help

    Sojwal, Nov 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    463
    Feroze [msft]
    Nov 10, 2004
  5. =?Utf-8?B?RXNyZWYgRFVSTkE=?=

    get image with httpwebrequest to stream to byte array to base64

    =?Utf-8?B?RXNyZWYgRFVSTkE=?=, Dec 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    5,870
    Joerg Jooss
    Dec 19, 2004
Loading...

Share This Page