HttpWebRequest.Credentials Proprietà

Definizione

Ottiene o imposta le informazioni sull'autenticazione per la richiesta.

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

Valore della proprietà

ICredentials

Oggetto ICredentials contenente le credenziali di autenticazione associate alla richiesta. Il valore predefinito è null.

Esempio

Nell'esempio di codice seguente vengono impostate le credenziali per una richiesta.

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

Commenti

La Credentials proprietà contiene informazioni di autenticazione per identificare l'autore della richiesta. La Credentials proprietà può essere , NetworkCredentialnel qual caso l'utente, la password e le informazioni di dominio contenute nell'oggetto NetworkCredential vengono usate per autenticare la richiesta oppure può essere un CredentialCache, nel qual caso l'URI (Uniform Resource Identifier) della richiesta viene usato per determinare l'utente, la password e le informazioni di dominio da utilizzare per autenticare la richiesta.

Nella maggior parte degli scenari client è consigliabile usare la DefaultCredentials proprietà , che contiene le credenziali dell'utente attualmente connesso. A tale scopo, impostare la UseDefaultCredentials proprietà su true anziché impostare questa proprietà.

Se la HttpWebRequest classe viene usata in un'applicazione di livello intermedio, ad esempio un'applicazione ASP.NET, le credenziali nella DefaultCredentials proprietà appartengono all'account che esegue la pagina ASP (le credenziali lato server). In genere, è necessario impostare questa proprietà sulle credenziali del client per conto del quale viene effettuata la richiesta.

Nota

Non è possibile utilizzare lo schema di autenticazione NTLM per rappresentare un altro utente. Kerberos deve essere configurato appositamente per supportare la rappresentazione.

Per limitare HttpWebRequest a uno o più metodi di autenticazione, usare la CredentialCache classe e associare le credenziali a uno o più schemi di autenticazione

Gli schemi di autenticazione supportati includono Digest, Negotiate, Kerberos, NTLM e Basic.

Per motivi di sicurezza, quando si seguono automaticamente i reindirizzamenti, archiviare le credenziali che si desidera includere nel reindirizzamento in un CredentialCache oggetto e assegnarlo a questa proprietà. Questa proprietà verrà impostata automaticamente su null al momento del reindirizzamento se contiene elementi ad eccezione di .CredentialCache Se questo valore della proprietà viene impostato automaticamente su null in queste condizioni, le credenziali non vengono inviate a una destinazione imprevista.

Si applica a