HttpWebRequest.Credentials Właściwość

Definicja

Pobiera lub ustawia informacje dotyczące 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

Element 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

Właściwość Credentials zawiera informacje uwierzytelniania identyfikujące twórcę żądania. Właściwość Credentials może być albo elementem NetworkCredential, w którym przypadku użytkownik, hasło i informacje o domenie zawarte w NetworkCredential obiekcie są używane do uwierzytelniania żądania lub może to być CredentialCacheelement , w którym to przypadku identyfikator URI żądania jest używany do określania użytkownika, hasła i informacji o domenie używanych do uwierzytelniania żądania.

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

HttpWebRequest Jeśli klasa jest używana w aplikacji warstwy środkowej, takiej jak aplikacja ASP.NET, poświadczenia w DefaultCredentials właściwości 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.

Uwaga

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ć właściwość HttpWebRequest do co najmniej jednej metody uwierzytelniania, użyj CredentialCache klasy i powiąż poświadczenia z co najmniej jednym schematem uwierzytelniania

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

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

Dotyczy