英語で読む

次の方法で共有


CredentialCache.DefaultCredentials プロパティ

定義

アプリケーションのシステム資格情報を取得します。

C#
public static System.Net.ICredentials DefaultCredentials { get; }

プロパティ値

アプリケーションのシステム資格情報を表す ICredentials

次のコード例では、 プロパティを DefaultCredentials 使用して、アプリケーションのシステム資格情報を取得します。

C#
// Ensure Directory Security settings for default web site in IIS is "Windows Authentication".
string url = "http://localhost";
// Create a 'HttpWebRequest' object with the specified url.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Assign the credentials of the logged in user or the user being impersonated.
myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
// Send the 'HttpWebRequest' and wait for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("Authentication successful");
Console.WriteLine("Response received successfully");

注釈

プロパティは DefaultCredentials 、NTLM、ネゴシエート、Kerberos ベースの認証にのみ適用されます。

DefaultCredentials は、アプリケーションが実行されている現在のセキュリティ コンテキストのシステム資格情報を表します。 クライアント側アプリケーションの場合、これらは通常、アプリケーションを実行しているユーザーの Windows 資格情報 (ユーザー名、パスワード、ドメイン) です。 ASP.NET アプリケーションの場合、既定の資格情報は、ログインしているユーザーのユーザー資格情報、または偽装されているユーザーです。

資格情報をインスタンスとして NetworkCredential 取得するには、 プロパティを DefaultNetworkCredentials 使用します。

でサポートされている値 authType は、"NTLM"、"Digest"、"Kerberos"、"Negotiate" です。 このメソッドは、HTTP プロトコルまたは FTP プロトコルでは機能しません。

注意

ICredentialsによってDefaultCredentials返されるインスタンスを使用して、現在のセキュリティ コンテキストのユーザー名、パスワード、またはドメインを表示することはできません。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0