HttpWebRequest.Credentials 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置请求的身份验证信息。
public:
virtual property System::Net::ICredentials ^ Credentials { System::Net::ICredentials ^ get(); void set(System::Net::ICredentials ^ value); };
public override System.Net.ICredentials Credentials { get; set; }
public override System.Net.ICredentials? Credentials { get; set; }
member this.Credentials : System.Net.ICredentials with get, set
Public Overrides Property Credentials As ICredentials
属性值
包含与请求关联的身份验证凭据的 ICredentials。 默认值为 null
。
示例
下面的代码示例设置请求的凭据。
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Text;
using namespace System::IO;
// Specify the URL to receive the request.
int main()
{
array<String^>^args = Environment::GetCommandLineArgs();
HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create(args[1]));
// Set some reasonable limits on resources used by this request
request->MaximumAutomaticRedirections = 4;
request->MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request->Credentials = CredentialCache::DefaultCredentials;
HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
Console::WriteLine("Content length is {0}", response->ContentLength);
Console::WriteLine("Content type is {0}", response->ContentType);
// Get the stream associated with the response.
Stream^ receiveStream = response->GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader^ readStream = gcnew StreamReader(receiveStream, Encoding::UTF8);
Console::WriteLine("Response stream received.");
Console::WriteLine(readStream->ReadToEnd());
response->Close();
readStream->Close();
}
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
using System;
using System.Net;
using System.Text;
using System.IO;
public class Test
{
// Specify the URL to receive the request.
public static void Main (string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Content length is {0}", response.ContentLength);
Console.WriteLine("Content type is {0}", response.ContentType);
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Console.WriteLine("Response stream received.");
Console.WriteLine(readStream.ReadToEnd());
response.Close();
readStream.Close();
}
}
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Test
' Specify the URL to receive the request.
Public Shared Sub Main(ByVal args() As String)
Dim request As HttpWebRequest = CType(WebRequest.Create(args(0)), HttpWebRequest)
' Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4
request.MaximumResponseHeadersLength = 4
' Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Console.WriteLine("Content length is {0}", response.ContentLength)
Console.WriteLine("Content type is {0}", response.ContentType)
' Get the stream associated with the response.
Dim receiveStream As Stream = response.GetResponseStream()
' Pipes the stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
Console.WriteLine("Response stream received.")
Console.WriteLine(readStream.ReadToEnd())
response.Close()
readStream.Close()
End Sub
End Class
'
'The output from this example will vary depending on the value passed into Main
'but will be similar to the following:
'
'Content length is 1542
'Content type is text/html; charset=utf-8
'Response stream received.
'...
'
'
注解
谨慎
WebRequest
、HttpWebRequest
、ServicePoint
和 WebClient
已过时,不应将其用于新开发。 请改用 HttpClient。
Credentials 属性包含身份验证信息,用于标识请求的创建者。 Credentials 属性可以是 NetworkCredential,在这种情况下,NetworkCredential 对象中包含的用户、密码和域信息用于对请求进行身份验证,也可以是 CredentialCache,在这种情况下,请求的统一资源标识符(URI)用于确定用于对请求进行身份验证的用户、密码和域信息。
在大多数客户端方案中,应使用 DefaultCredentials 属性,该属性包含当前登录用户的凭据。 为此,请将 UseDefaultCredentials 属性设置为 true
,而不是设置此属性。
如果在中间层应用程序中(如 ASP.NET 应用程序)中使用 HttpWebRequest 类,则 DefaultCredentials 属性中的凭据属于运行 ASP 页的帐户(服务器端凭据)。 通常,将此属性设置为代表发出请求的客户端的凭据。
注意
NTLM 身份验证方案不能用于模拟其他用户。 必须专门配置 Kerberos 以支持模拟。
若要将 HttpWebRequest 限制为一个或多个身份验证方法,请使用 CredentialCache 类并将凭据绑定到一个或多个身份验证方案
支持的身份验证方案包括 Digest、Negotiate、Kerberos、NTLM 和 Basic。
出于安全原因,在自动关注重定向时,请将要包含在重定向中的凭据存储在 CredentialCache 中,并将其分配给此属性。 如果此属性包含除 CredentialCache以外的任何内容,则此属性将自动设置为在重定向时 null
。 在这些情况下,将此属性值自动设置为 null
可防止将凭据发送到任何意外的目标。