401.1 Error when calling an asp.net page from code.

Giganews Newsgroups
Subject: 401.1 Error when calling an asp.net page from code.
Posted by:  blevitt
Date: Thu, 30 Jul 2009

aspx page A and aspx page B are on the same server.  I'm using the code below
in aspx page A to call aspx page B.  This works fine in our qa and dev
environments but fails in our production environment.  Using the default
credentials also works in dev and qa.  In production, the
httpRequest.GetResponse() call throws an error and looking at the
WebException response it shows page be responded with:
HTTP Error 401.1 - Unauthorized: Access is denied due to invalid credentials.

If I connect to page B directly it works fine (even if using the same
credentials I'm using in code).  When using DefaultCredentials I thought that
I had an NTLM double hop issue, but adding credentials voids that I think.
I'm running out of ideas.  The only other difference on this box that I'm
aware of is that it is load balanced (although at the moment it only has one
node).

    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(helperURL);
    httpRequest.Credentials = new NetworkCredential("username", "password");
    //httpRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
    //httpRequest.UseDefaultCredentials = true;
    //httpRequest.PreAuthenticate = true;

    StreamReader reader;
    XmlDocument xmlDoc = new XmlDocument();
    try
    {
      using (HttpWebResponse httpResponse =
(HttpWebResponse)httpRequest.GetResponse())
      {
        reader = new StreamReader(httpResponse.GetResponseStream());
        xmlDoc.Load(reader);
      }
    }
    catch (System.Net.WebException ex)
    {
      throw new ApplicationException(
        "Data helper threw http 500 error:\r
"
        + DumpHttp(httpRequest, ex.Response)
      );
    }

Replies