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 、要求の作成者を識別するための認証情報が含まれています。 プロパティには CredentialsNetworkCredentialを指定できます。この場合、オブジェクトに含まれる NetworkCredential ユーザー、パスワード、およびドメイン情報を使用して要求を認証するか、 を指定できます CredentialCache。この場合、要求の URI (Uniform Resource Identifier) を使用して、要求の認証に使用するユーザー、パスワード、およびドメイン情報を決定します。

ほとんどのクライアント シナリオでは、 プロパティを DefaultCredentials 使用する必要があります。このプロパティには、現在ログオンしているユーザーの資格情報が含まれています。 これを行うには、このプロパティを UseDefaultCredentials 設定する代わりに、 プロパティを に true 設定します。

クラスが HttpWebRequest 中間層アプリケーション (ASP.NET アプリケーションなど) で使用されている場合、プロパティの DefaultCredentials 資格情報は ASP ページを実行しているアカウント (サーバー側の資格情報) に属します。 通常、このプロパティは、要求が行われたクライアントの資格情報に設定します。

注意

NTLM 認証スキームを使用して別のユーザーを偽装することはできません。 Kerberos は、偽装をサポートするように特別に構成する必要があります。

HttpWebRequest を 1 つ以上の認証方法に制限するには、 クラスを CredentialCache 使用し、資格情報を 1 つ以上の認証スキームにバインドします

サポートされている認証スキームには、ダイジェスト、ネゴシエート、Kerberos、NTLM、Basic が含まれます。

セキュリティ上の理由から、リダイレクトを自動的にフォローする場合は、リダイレクトに含める資格情報を に CredentialCache 格納し、このプロパティに割り当てます。 を除く何かを含む場合、このプロパティはリダイレクト時に自動的に CredentialCachenull設定されます。 これらの条件下でこのプロパティ値を に自動的に null 設定すると、資格情報が意図しない宛先に送信されなくなります。

適用対象