Udostępnij za pośrednictwem


HttpWebRequest.Credentials Właściwość

Definicja

Pobiera lub ustawia informacje uwierzytelniania dla żądania.

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

Wartość właściwości

ICredentials zawierający poświadczenia uwierzytelniania skojarzone z żądaniem. Wartość domyślna to null.

Przykłady

Poniższy przykład kodu ustawia poświadczenia dla żądania.

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

Uwagi

Ostrożność

WebRequest, HttpWebRequest, ServicePointi WebClient są przestarzałe i nie należy ich używać do tworzenia nowych aplikacji. Zamiast tego użyj HttpClient.

Właściwość Credentials zawiera informacje o uwierzytelnianiu w celu zidentyfikowania twórcy żądania. Właściwość Credentials może być NetworkCredential, w tym przypadku użytkownik, hasło i informacje o domenie zawarte w obiekcie NetworkCredential są używane do uwierzytelniania żądania lub może to być CredentialCache, w którym przypadku identyfikator URI żądania jest używany do określania użytkownika, hasła i informacji o domenie do uwierzytelniania żądania.

W większości scenariuszy klienta należy użyć właściwości DefaultCredentials, która zawiera poświadczenia aktualnie zalogowanego użytkownika. W tym celu ustaw właściwość UseDefaultCredentials na true zamiast ustawiać tę właściwość.

Jeśli klasa HttpWebRequest jest używana w aplikacji warstwy środkowej, takiej jak aplikacja ASP.NET, poświadczenia we właściwości DefaultCredentials należą do konta z uruchomioną stroną ASP (poświadczenia po stronie serwera). Zazwyczaj tę właściwość należy ustawić na poświadczenia klienta, którego imieniu jest wykonywane żądanie.

Nuta

Schemat uwierzytelniania NTLM nie może być używany do personifikacji innego użytkownika. Protokół Kerberos musi być specjalnie skonfigurowany do obsługi personifikacji.

Aby ograniczyć metodę HttpWebRequest do co najmniej jednej metody uwierzytelniania, użyj klasy CredentialCache i powiąż poświadczenia z co najmniej jednym schematem uwierzytelniania

Obsługiwane schematy uwierzytelniania obejmują skrót, negocjacja, kerberos, NTLM i podstawową.

Ze względów bezpieczeństwa podczas automatycznego wykonywania przekierowań zapisz poświadczenia, które mają zostać uwzględnione w przekierowaniu w CredentialCache i przypisz je do tej właściwości. Ta właściwość zostanie automatycznie ustawiona na null po przekierowaniu, jeśli zawiera wszystkie elementy z wyjątkiem CredentialCache. Ustawienie tej wartości właściwości na wartość null w tych warunkach uniemożliwia wysyłanie poświadczeń do niezamierzonego miejsca docelowego.

Dotyczy