Прочетете на английски Редактиране

Споделяне чрез


XmlUrlResolver.Credentials Property

Definition

Sets credentials used to authenticate web requests.

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public override System.Net.ICredentials Credentials { set; }
C#
public override System.Net.ICredentials Credentials { set; }

Property Value

The credentials to be used to authenticate web requests. If this property is not set, the value defaults to null; that is, the XmlUrlResolver has no user credentials.

Attributes

Examples

The following example creates an XmlUrlResolver object with credentials. The XmlReader uses the credentials on the XmlUrlResolver object to access a network resource.

C#

// Create a resolver and specify the necessary credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
System.Net.NetworkCredential myCred;
myCred  = new System.Net.NetworkCredential(UserName,SecurelyStoredPassword,Domain);
resolver.Credentials = myCred;

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.

You can associate different credentials with different URIs and add them to a cache. You can then use the credentials to check authentication for different URIs, regardless of the original source of the XML.

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

C#
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;  

Applies to

Продукт Версии
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also