XmlUrlResolver.Credentials 属性

定义

设置用于对 Web 请求进行身份验证的凭据。

public:
 virtual property System::Net::ICredentials ^ Credentials {  void set(System::Net::ICredentials ^ value); };
public override System.Net.ICredentials Credentials { set; }
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public override System.Net.ICredentials Credentials { set; }
member this.Credentials : System.Net.ICredentials
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Credentials : System.Net.ICredentials
Public Overrides Property Credentials As ICredentials

属性值

用于对 Web 请求进行身份验证的凭据。 如果未设置该属性,则该值默认为 null;即 XmlUrlResolver 没有用户凭据。

属性

示例

以下示例使用凭据创建对象 XmlUrlResolverXmlReader使用 对象上的XmlUrlResolver凭据来访问网络资源。


// 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;
    ' Create a resolver and specify the necessary credentials.
    Dim resolver As New XmlUrlResolver()
    Dim myCred As System.Net.NetworkCredential
    myCred = New System.Net.NetworkCredential(UserName, SecurelyStoredPassword, Domain)
    resolver.Credentials = myCred

注解

如果虚拟目录不需要身份验证,则无需设置此属性。 否则,必须提供用户的凭据。

可以将不同的凭据与不同的 URI 相关联,并将其添加到缓存。 然后,可以使用凭据检查不同 URI 的身份验证,而不考虑 XML 的原始源。

以下 C# 代码演示如何将 Credentials 属性设置为凭据缓存。

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;  

适用于

另请参阅