Access remote xml file using Credentials

From: Ianb (Ianb_at_discussions.microsoft.com)
Date: 01/20/05


Date: Thu, 20 Jan 2005 12:27:03 -0800

Hi

I'm trying to access an xml document outside my site root to do some
manipulation and I'm getting an error (on the while statement):

The remote server returned an error: (401) Unauthorized.

I guess this is because I don't have permissions on the file but I've been
trying to set credentials before the file access. Can any one see what I'm
doing wrong - Heres my code:
------------------------------------
WebClient oWebClient = new WebClient();
oWebClient.Credentials = CredentialCache.DefaultCredentials;

string sResource = docRecord.Filepath + docRecord.Filename;
Stream oStream = oWebClient.OpenRead(sResource);

StreamReader oSR = new StreamReader(oStream);
        
// create an instance of the XmlDocument object
  XmlTextReader oXmlRdr = new XmlTextReader(oSR.ReadToEnd());

  oXmlRdr.WhitespaceHandling = WhitespaceHandling.None;
  while(oXmlRdr.Read()) {
// ... xml manipulation
  }

// close the object and free up memory
  oXmlRdr.Close();
  oStream.Close();
-----------------------------------
and corresponding web.config entry:

    <authentication mode = "Forms">
      <forms>
        <credentials passwordFormat = "Clear">
          <user name ="ianb" password = "******"/>
        </credentials>
      </forms>
    </authentication>
    <authorization>
      <deny users = "?"/>
    </authorization>

===============================
I have also tried the following and got the same result:
  WebClient oWebClient = new WebClient();
  NetworkCredential oCred = new NetworkCredential("ianb", "******");
  oWebClient.Credentials = oCred;
  String strURL = "http://validURL/XMLPage.xml";
  Stream oStream = oWebClient.OpenRead(strURL);
  StreamReader oSR = new StreamReader(oStream);

  XmlTextReader oXmlRdr = new XmlTextReader(oSR.ReadToEnd());
  oXmlRdr.WhitespaceHandling = WhitespaceHandling.None;

  while(oXmlRdr.Read()) {
// ... xml manipulation
  }

// close the object and free up memory
  oXmlRdr.Close();
  oStream.Close();
-------------------------------------
I would prefer to use the web.config option. Can anyone help me with this

Thanks

Ian B



Relevant Pages

  • Access remote xml file using Credentials
    ... I'm trying to access an xml document outside my site root to do some ... manipulation and I'm getting an error: ... WebClient oWebClient = new WebClient; ... Stream oStream = oWebClient.OpenRead; ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Access remote xml file using Credentials
    ... I'm trying to access an xml document outside my site root to do some ... manipulation and I'm getting an error: ... WebClient oWebClient = new WebClient; ... Stream oStream = oWebClient.OpenRead; ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Technologies to teach in undergrad CS
    ... As bad as working without an RCS is, ... I haven't learned XML and the associated tools yet, ... to use various traditional Unix text manipulation tools. ... using a modern debugger. ...
    (comp.edu)
  • Re: Fastcode Memory Manager Testing Status
    ... > No dependency on external libraries and MS XML compatible. ...
    (borland.public.delphi.language.basm)