Mono Class Library: System.Xml.XmlUrlResolver Overview | Members

System.Xml.XmlUrlResolver.Credentials Property

Sets the credentials used to authenticate Web requests. [Edit]

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

Value

A System.Net.ICredentials instance containing the credentials. [Edit]

Remarks

This property is write-only.

If the virtual directory is configured to allow anonymous access, credentials are not needed and this property does not need to be set. If credentials are needed but not supplied, the System.Xml.XmlUrlResolver class uses default credentials (System.Net.CredentialCache.DefaultCredentials).

Note:

This property overrides XmlResolver.Credentials.

[Edit]

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