HttpWebRequest.Credentials 속성

정의

요청에 대한 인증 정보를 가져오거나 설정합니다.

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

속성 값

요청과 관련된 인증 자격 증명이 들어 있는 ICredentials입니다. 기본값은 null입니다.

예제

다음 코드 예제에서는 요청에 대한 자격 증명을 설정합니다.

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

설명

속성에는 Credentials 요청 작성자를 식별하는 인증 정보가 포함되어 있습니다. 속성은 Credentials 일 수 있습니다 NetworkCredential는 , 이 경우 사용자, 암호 및 도메인 정보 개체를 인증 NetworkCredential 하는 데 사용 되는 요청을 인증 하는 데 사용 됩니다 또는 CredentialCache, 이 경우 요청의 URI (Uniform Resource Identifier)는 요청을 인증 하는 데 사용할 사용자, 암호 및 도메인 정보를 확인 하는 데 사용 됩니다.

대부분의 클라이언트 시나리오에서는 현재 로그온한 사용자의 자격 증명을 포함하는 속성을 사용해야 DefaultCredentials 합니다. 이렇게 하려면 이 속성을 설정하는 대신 속성을 로 true 설정합니다UseDefaultCredentials.

경우는 HttpWebRequest 클래스는 ASP.NET 애플리케이션에서 자격 증명 같은 중간 계층 애플리케이션에서 사용 되는 DefaultCredentials 속성 ASP 페이지 (서버 쪽 자격 증명)를 실행 하는 계정에 속해야 합니다. 일반적으로 이 속성을 대신 요청이 수행되는 클라이언트의 자격 증명으로 설정합니다.

참고

NTLM 인증 체계는 다른 사용자를 가장하는 데 사용할 수 없습니다. 가장을 지원하도록 Kerberos를 특별히 구성해야 합니다.

HttpWebRequest를 하나 이상의 인증 방법으로 제한하려면 클래스를 CredentialCache 사용하고 자격 증명을 하나 이상의 인증 체계에 바인딩합니다.

지원되는 인증 체계에는 다이제스트, 협상, Kerberos, NTLM 및 Basic이 포함됩니다.

보안상의 이유로 리디렉션을 자동으로 팔로우할 때 리디렉션에 포함할 자격 증명을 CredentialCache 에 저장하고 이 속성에 할당합니다. 이 속성은 를 제외한 CredentialCache모든 항목이 포함된 경우 리디렉션 시 자동으로 로 설정 null 됩니다. 이러한 조건에서 이 속성 값을 자동으로 로 설정 null 하면 자격 증명이 의도하지 않은 대상으로 전송되지 않습니다.

적용 대상