System.Xml.XmlUrlResolver.Credentials Property

Sets credentials used to authenticate Web requests.

Syntax

public override System.Net.ICredentials Credentials { set; }

Value

A System.Net.ICredentials instance containing the credentials.

Remarks

If the virtual directory does not require authentication, this property does not need to be set. Otherwise, the credentials of the user must be supplied.

Different credentials can be associated with different URIs and added to a credential cache. The credentials can then be used to check authentication for different URIs, regardless of the original source of the XML.

The following C# code shows how to set the XmlUrlResolver.Credentials property to a credential cache.

Example

NetworkCredential myCred = new NetworkCredential(UserName,SecurelyStoredPassword,Domain); 
CredentialCache myCache = new CredentialCache(); 
myCache.Add(new Uri("http://www.contoso.com/"), "Basic", myCred); 
myCache.Add(new Uri("http://app.contoso.com/"), "Basic", myCred);
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = myCache;

Example

The following example sets credentials for the virtual directory "http://localhost/bookstore/inventory.xml". There is no output from this example.

C# Example

using System;
using System.Net;
using System.Xml;

public class App {

  public static void Main() {

    XmlTextReader xtReader =  
      new XmlTextReader("http://localhost/" +
                        "bookstore/inventory.xml");
    NetworkCredential netCredential =
      new NetworkCredential("username",
                            "password",
                            "domain");
    XmlUrlResolver xResolver = new XmlUrlResolver();
    xResolver.Credentials = netCredential;
    xtReader.XmlResolver= xResolver;
  }
}
   

The following example associates different credentials with different URIs and adds the credentials to a credential cache. The credentials can then be used to check authentication for different URIs regardless of the original source of the XML. There is no output from this example.

C# Example

using System;
using System.Net;
using System.Xml;

public class App {

  public static void Main() {

    XmlTextReader xtReader =  
      new XmlTextReader("http://localhost/" +
                        "bookstore/inventory.xml");
    NetworkCredential netCredential1 =
      new NetworkCredential("username1",
                            "password1",
                            "domain1");
    NetworkCredential netCredential2 =
      new NetworkCredential("username2",
                            "password2",
                            "domain2");
    CredentialCache credCache = new CredentialCache(); 
    credCache.Add( new Uri("http://www.contoso.com/"),
                  "Basic",
                   netCredential1); 
    credCache.Add( new Uri("http://app.contoso.com/"),
                  "Basic",
                   netCredential2);
    XmlUrlResolver xResolver = new XmlUrlResolver();
    xResolver.Credentials = credCache;
    xtReader.XmlResolver= xResolver;
  }
}
   

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0