共用方式為


CredentialCache.DefaultCredentials 屬性

定義

取得應用程式的系統認證。

public:
 static property System::Net::ICredentials ^ DefaultCredentials { System::Net::ICredentials ^ get(); };
public static System.Net.ICredentials DefaultCredentials { get; }
static member DefaultCredentials : System.Net.ICredentials
Public Shared ReadOnly Property DefaultCredentials As ICredentials

屬性值

ICredentials,表示應用程式的系統認證。

範例

下列程式代碼範例會 DefaultCredentials 使用 屬性來取得應用程式的系統認證。

// 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 = dynamic_cast<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 = dynamic_cast<HttpWebResponse^>(myHttpWebRequest->GetResponse());
Console::WriteLine( "Authentication successful" );
Console::WriteLine( "Response received successfully" );
// 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");
' Assuming "Windows Authentication" has been set as; 
' Directory Security settings for default web site in IIS.
Dim url As String = "http://localhost"
' Create a 'HttpWebRequest' object with the specified url. 
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
' Assign the credentials of the logged in user or the user being impersonated.
myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials
' Send the 'HttpWebRequest' and wait for response.            
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
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 通訊協定。

注意

ICredentialsDefaultCredentials回的 實例無法用來檢視目前安全性內容的使用者名稱、密碼或網域。

適用於