XmlResolver.Credentials 属性

定义

当在派生类中被重写时,设置用于对 Web 请求进行身份验证的凭据。When overridden in a derived class, sets the credentials used to authenticate web requests.

public:
 virtual property System::Net::ICredentials ^ Credentials {  void set(System::Net::ICredentials ^ value); };
public:
 abstract property System::Net::ICredentials ^ Credentials {  void set(System::Net::ICredentials ^ value); };
public virtual System.Net.ICredentials Credentials { set; }
public abstract System.Net.ICredentials Credentials { set; }
member this.Credentials : System.Net.ICredentials
Public Overridable Property Credentials As ICredentials
Public MustOverride Property Credentials As ICredentials

属性值

ICredentials

用于对 Web 请求进行身份验证的凭据。The credentials to be used to authenticate web requests. 如果未设置该属性,则该值默认为 null;即 XmlResolver 没有用户凭据。If this property is not set, the value defaults to null; that is, the XmlResolver has no user credentials.

示例

XmlUrlResolver.Credentials有关使用此属性的示例,请参阅。See XmlUrlResolver.Credentials for an example that uses this property.

注解

如果虚拟目录不需要身份验证,则无需设置此属性。If the virtual directory does not require authentication, this property doesn't have to be set. 否则,必须提供用户的凭据。Otherwise, the credentials of the user must be supplied.

下面的 c # 代码在对象上设置凭据 XmlResolverThe following C# code sets credentials on the XmlResolver object.

NetworkCredential nc = new NetworkCredential(UserName, SecurelyStoredPassword, Domain);  
XmlUrlResolver resolver = new XmlUrlResolver();  
resolver.Credentials = nc;  

可以将不同的凭据与不同的 Uri 相关联,并将其添加到缓存中。You can associate different credentials with different URIs and add them to a cache. 然后,你可以使用凭据来检查不同 Uri 的身份验证,而与 XML 的原始源无关。You can then use the credentials to check authentication for different URIs regardless of the original source of the XML.

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;  

适用于

另请参阅