Share via


HttpWebRequest.Credentials Özellik

Tanım

İstek için kimlik doğrulama bilgilerini alır veya ayarlar.

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

Özellik Değeri

ICredentials İstekle ilişkili kimlik doğrulama kimlik bilgilerini içeren. Varsayılan değer: null.

Örnekler

Aşağıdaki kod örneği bir isteğin kimlik bilgilerini ayarlar.

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

Açıklamalar

özelliği, Credentials isteğin oluşturucusunun kimliğini belirlemek için kimlik doğrulama bilgilerini içerir. Credentials özelliği bir NetworkCredentialolabilir; bu durumda nesnede NetworkCredential yer alan kullanıcı, parola ve etki alanı bilgileri isteğin kimliğini doğrulamak için kullanılır veya isteğin CredentialCacheTekdüzen Kaynak Tanımlayıcısı (URI) isteğin kimliğini doğrulamak için kullanılacak kullanıcı, parola ve etki alanı bilgilerini belirlemek için kullanılır.

Çoğu istemci senaryosunda DefaultCredentials , şu anda oturum açmış olan kullanıcının kimlik bilgilerini içeren özelliğini kullanmanız gerekir. Bunu yapmak için, bu özelliği ayarlamak yerine özelliğini true olarak ayarlayınUseDefaultCredentials.

HttpWebRequest Sınıf, ASP.NET uygulaması gibi bir orta katman uygulamasında kullanılıyorsa, özelliğindeki DefaultCredentials kimlik bilgileri ASP sayfasını (sunucu tarafı kimlik bilgileri) çalıştıran hesaba aittir. Genellikle, bu özelliği isteğin yapıldığı istemcinin kimlik bilgilerine ayarlarsınız.

Not

NTLM kimlik doğrulama düzeni başka bir kullanıcının kimliğine bürünmek için kullanılamaz. Kerberos, kimliğe bürünme özelliğini destekleyecek şekilde özel olarak yapılandırılmalıdır.

HttpWebRequest'i bir veya daha fazla kimlik doğrulama yöntemiyle kısıtlamak için sınıfını CredentialCache kullanın ve kimlik bilgilerinizi bir veya daha fazla kimlik doğrulama düzenine bağlayın

Desteklenen kimlik doğrulama düzenleri Özet, Anlaşma, Kerberos, NTLM ve Temel'i içerir.

Güvenlik nedenleriyle, yeniden yönlendirmeleri otomatik olarak takip ederken, yeniden yönlendirmeye eklenmesini istediğiniz kimlik bilgilerini bir CredentialCache içinde depolayın ve bu özelliğe atayın. Bu özellik, bir dışında herhangi bir CredentialCacheşey içeriyorsa yeniden yönlendirme sırasında otomatik olarak olarak ayarlanırnull. Bu özellik değerinin bu koşullar altında otomatik olarak olarak ayarlanması null , kimlik bilgilerinin istenmeyen herhangi bir hedefe gönderilmesini önler.

Şunlara uygulanır