CredentialCache.DefaultCredentials 屬性

定義

取得應用程式的系統認證。Gets the system credentials of the application.

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

屬性值

ICredentials

ICredentials,表示應用程式的系統認證。An ICredentials that represents the system credentials of the application.

範例

下列程式碼範例會使用 DefaultCredentials 屬性來取得應用程式的系統認證。The following code example uses the DefaultCredentials property to get the system credentials of the application.

// 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、negotiate 和 Kerberos 型驗證。The DefaultCredentials property applies only to NTLM, negotiate, and Kerberos-based authentication.

DefaultCredentials表示正在執行應用程式之目前安全性內容的系統認證。DefaultCredentials represents the system credentials for the current security context in which the application is running. 針對用戶端應用程式,這些通常是執行應用程式之使用者的使用者名稱、密碼和網域) (的 Windows 認證。For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application. 對於 ASP.NET 應用程式,預設認證是已登入使用者或正在模擬之使用者的使用者認證。For ASP.NET applications, the default credentials are the user credentials of the logged-in user, or the user being impersonated.

若要取得做為實例的認證 NetworkCredential ,請使用 DefaultNetworkCredentials 屬性。To get the credentials as a NetworkCredential instance, use the DefaultNetworkCredentials property.

支援的值為「 authType NTLM」、「摘要」、「Kerberos」和「Negotiate」。The supported values for authType are "NTLM", "Digest", "Kerberos", and "Negotiate". 這個方法不適用於 HTTP 或 FTP 通訊協定。This method does not work for HTTP or FTP protocols.

注意

ICredentials 傳回的實例 DefaultCredentials 無法用來查看目前安全性內容的使用者名稱、密碼或網域。The ICredentials instance returned by DefaultCredentials cannot be used to view the user name, password, or domain of the current security context.

適用於