WebClient.Credentials 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置发送到主机并用于对请求进行身份验证的网络凭据。
public:
property System::Net::ICredentials ^ Credentials { System::Net::ICredentials ^ get(); void set(System::Net::ICredentials ^ value); };
public System.Net.ICredentials? Credentials { get; set; }
public System.Net.ICredentials Credentials { get; set; }
member this.Credentials : System.Net.ICredentials with get, set
Public Property Credentials As ICredentials
属性值
一个 ICredentials,包含该请求的身份验证凭据。 默认值为 null
。
示例
以下代码示例使用用户的系统凭据对请求进行身份验证。
int main()
{
try
{
WebClient^ client = gcnew WebClient;
client->Credentials = CredentialCache::DefaultCredentials;
array<Byte>^pageData = client->DownloadData( "http://www.contoso.com" );
String^ pageHtml = Encoding::ASCII->GetString( pageData );
Console::WriteLine( pageHtml );
}
catch ( WebException^ webEx )
{
Console::Write( webEx );
}
}
public static void Main()
{
try {
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
Byte[] pageData = client.DownloadData("http://www.contoso.com");
string pageHtml = Encoding.ASCII.GetString(pageData);
Console.WriteLine(pageHtml);
} catch (WebException webEx) {
Console.Write(webEx.ToString());
}
}
Public Shared Sub Main()
Try
Dim client As New WebClient()
client.Credentials = CredentialCache.DefaultCredentials
Dim pageData As [Byte]() = client.DownloadData("http://www.contoso.com")
Dim pageHtml As String = Encoding.ASCII.GetString(pageData)
Console.WriteLine(pageHtml)
Catch webEx As WebException
Console.Write(webEx.ToString())
End Try
End Sub
注解
属性 Credentials 包含用于访问主机上资源的身份验证凭据。 在大多数客户端方案中,应使用 DefaultCredentials,这是当前登录用户的凭据。 为此,请将 UseDefaultCredentials 属性设置为 true
,而不是设置此属性。
WebClient如果在中间层应用程序(如 ASP.NET 应用程序)中使用类,则 DefaultCredentials 属于运行 ASP 页的帐户, (服务器端凭据) 。 通常,将此属性设置为代表其发出请求的客户端的凭据。
出于安全原因,在自动跟踪重定向时,请将要包含在重定向中的凭据存储在 中, CredentialCache 并将其分配给此属性。 如果此属性包含除 以外的CredentialCache任何内容,则重定向时,此属性将自动设置为 null
。 在这些情况下,将此属性值自动设置为 null
会阻止将凭据发送到任何意外目标。
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈