HttpWebRequest.Credentials プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > HttpWebRequest.Credentials プロパティの意味・解説 

HttpWebRequest.Credentials プロパティ

要求に対して使用する認証情報を取得または設定します。

名前空間: System.Net
アセンブリ: System (system.dll 内)
構文構文

Public Overrides Property
 Credentials As ICredentials
Dim instance As HttpWebRequest
Dim value As ICredentials

value = instance.Credentials

instance.Credentials = value
public override ICredentials Credentials { get;
 set; }
public:
virtual property ICredentials^ Credentials {
    ICredentials^ get () override;
    void set (ICredentials^ value) override;
}
/** @property */
public ICredentials get_Credentials ()

/** @property */
public void set_Credentials (ICredentials value)

プロパティ値
要求と関連付けられた認証資格情報を格納する ICredentials。既定値は null 参照 (Visual Basic では Nothing) です。

解説解説

Credentials プロパティは、要求の作成者を識別する認証情報を格納します。Credentials プロパティには、NetworkCredential (この場合は、NetworkCredential オブジェクトに格納されているユーザー、パスワード、およびドメイン情報を使用して、要求を認証します) または CredentialCache (この場合は、要求の URI (Uniform Resource Identifier) を使用して、その要求の認証に使用するユーザー、パスワード、およびドメイン情報を決定します) のいずれかを指定できます。

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

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

メモメモ

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

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

サポートされる認証方式には、Digest、Negotiate、Kerberos、NTLM、Basic などがあります。

使用例使用例

要求に必要な資格情報を設定するコード例を次に示します。

Imports System
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 'Main
End Class 'Test
'
'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>

*/
#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>

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「HttpWebRequest.Credentials プロパティ」の関連用語

HttpWebRequest.Credentials プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



HttpWebRequest.Credentials プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS