HttpWebRequest.Credentials Vlastnost

Definice

Získá nebo nastaví ověřovací informace pro požadavek.

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

Hodnota vlastnosti

Obsahuje ICredentials ověřovací přihlašovací údaje přidružené k požadavku. Výchozí formát je null.

Příklady

Následující příklad kódu nastaví přihlašovací údaje pro požadavek.

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

Poznámky

Vlastnost Credentials obsahuje ověřovací informace k identifikaci tvůrce požadavku. Vlastnost Credentials může být buď NetworkCredential, v takovém případě se k ověření požadavku použije uživatel, heslo a informace o doméně obsažené v NetworkCredential objektu CredentialCache, nebo může být , v takovém případě se k určení uživatele, hesla a informací o doméně, které se mají použít k ověření požadavku, použije identifikátor URI (Uniform Resource Identifier) požadavku.

Ve většině klientských scénářů byste měli použít DefaultCredentials vlastnost, která obsahuje přihlašovací údaje aktuálně přihlášeného uživatele. Chcete-li to provést, nastavte UseDefaultCredentials vlastnost na true hodnotu namísto nastavení této vlastnosti.

HttpWebRequest Pokud se třída používá v aplikaci střední vrstvy, například v aplikaci ASP.NET, patří přihlašovací údaje ve DefaultCredentials vlastnosti účtu, na kterém běží stránka ASP (přihlašovací údaje na straně serveru). Obvykle byste tuto vlastnost nastavili na přihlašovací údaje klienta, jehož jménem je žádost provedena.

Poznámka

Schéma ověřování NTLM nelze použít k zosobnění jiného uživatele. Protokol Kerberos musí být speciálně nakonfigurovaný tak, aby podporoval zosobnění.

Pokud chcete httpWebRequest omezit na jednu nebo více metod ověřování, použijte CredentialCache třídu a vytvořte vazbu přihlašovacích údajů k jednomu nebo více schématům ověřování.

Mezi podporovaná schémata ověřování patří Digest, Negotiate, Kerberos, NTLM a Basic.

Z bezpečnostních důvodů při automatickém sledování přesměrování uložte přihlašovací údaje, které chcete zahrnout do přesměrování, a CredentialCache přiřaďte je k této vlastnosti. Tato vlastnost se automaticky nastaví na null při přesměrování, pokud obsahuje cokoli kromě CredentialCache. Automatické nastavení null této hodnoty vlastnosti za těchto podmínek brání odesílání přihlašovacích údajů do jakéhokoli nezamýšleného cíle.

Platí pro