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

屬性值

ICredentials

ICredentials,包含與要求關聯的驗證認證。An 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. 如果重新導向時 null 包含以外的任何專案,此屬性會自動設定為 CredentialCacheThis 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.

適用於