Access remote xml file using Credentials

Discussion in 'ASP .Net Security' started by Ianb, Jan 20, 2005.

  1. Ianb

    Ianb Guest

    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
     
    Ianb, Jan 20, 2005
    #1
    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. Craig Deelsnyder

    Re: Pass Basic Auth. credentials to remote site?

    Craig Deelsnyder, Jul 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    661
    Craig Deelsnyder
    Jul 21, 2004
  2. Andy Klare

    File.Move() using different credentials?

    Andy Klare, Nov 30, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    763
    Andy Klare
    Nov 30, 2006
  3. Olli Plough
    Replies:
    2
    Views:
    247
    Martin Gregorie
    Dec 1, 2007
  4. Ianb

    Access remote xml file using Credentials

    Ianb, Jan 20, 2005, in forum: ASP .Net Security
    Replies:
    7
    Views:
    181
    Joe Kaplan \(MVP - ADSI\)
    Jan 21, 2005
  5. Sandworth Meb
    Replies:
    0
    Views:
    121
    Sandworth Meb
    Aug 29, 2009
Loading...

Share This Page