HttpWebRequest.Credentials 属性

定义

获取或设置请求的身份验证信息。Gets or sets authentication information for the request.

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; }
member this.Credentials : System.Net.ICredentials with get, set
Public Overrides Property Credentials As ICredentials

属性值

包含与该请求关联的身份验证凭据的 ICredentialsAn ICredentials that contains the authentication credentials associated with the request. 默认值为 nullThe default is null.

示例

下面的代码示例设置请求的凭据。The following code example sets the credentials for a request.

#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.
'...
'
'

注解

Credentials 属性包含标识请求制造商的身份验证信息。The Credentials property contains authentication information to identify the maker of the request. Credentials 属性可以是 NetworkCredential,在这种情况下,NetworkCredential 对象中包含的用户、密码和域信息可用于对 CredentialCache请求进行身份验证,在这种情况下,请求的统一资源标识符(URI)用于确定用于对请求进行身份验证的用户、密码和域信息。The Credentials property can be either a NetworkCredential, in which case the user, password, and domain information contained in the NetworkCredential object is used to authenticate the request, or it can be a CredentialCache, in which case the Uniform Resource Identifier (URI) of the request is used to determine the user, password, and domain information to use to authenticate the request.

在大多数客户端方案中,应使用 DefaultCredentials 属性,其中包含当前登录的用户的凭据。In most client scenarios, you should use the DefaultCredentials property, which contains the credentials of the currently logged on user. 为此,请将 UseDefaultCredentials 属性设置为 true,而不是设置此属性。To do this, set the UseDefaultCredentials property to true instead of setting this property.

如果在中间层应用程序中使用 HttpWebRequest 类(如 ASP.NET 应用程序),则 DefaultCredentials 属性中的凭据属于运行 ASP 页(服务器端凭据)的帐户。If the HttpWebRequest class is being used in a middle-tier application, such as an ASP.NET application, the credentials in the DefaultCredentials property belong to the account running the ASP page (the server-side credentials). 通常情况下,会将此属性设置为代表其发出请求的客户端的凭据。Typically, you would set this property to the credentials of the client on whose behalf the request is made.

备注

NTLM 身份验证方案不能用于模拟其他用户。The NTLM authentication scheme cannot be used to impersonate another user. 必须将 Kerberos 专门配置为支持模拟。Kerberos must be specially configured to support impersonation.

若要将 HttpWebRequest 限制为一个或多个身份验证方法,请使用 CredentialCache 类,并将凭据绑定到一个或多个身份验证方案To restrict HttpWebRequest to one or more authentication methods, use the CredentialCache class and bind your credentials to one or more authentication schemes

支持的身份验证方案包括摘要式、Negotiate、Kerberos、NTLM 和 Basic。Supported authentication schemes include Digest, Negotiate, Kerberos, NTLM, and Basic.

出于安全原因,在自动重定向后,请存储要在 CredentialCache 中包含的凭据,并将其分配给此属性。For security reasons, when automatically following redirects, store the credentials that you want to be included in the redirect in a CredentialCache and assign it to this property. 如果重定向包含除 CredentialCache之外的任何内容,则此属性将自动设置为 null 重定向。This property will automatically be set to null upon redirection if it contains anything except a CredentialCache. 在这些条件下,将此属性值自动设置为 null 会阻止将凭据发送到任何意外的目标。Having this property value be automatically set to null under those conditions prevents credentials from being sent to any unintended destination.

适用于