CredentialCache.DefaultCredentials 屬性
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得應用程式的系統認證。
public:
static property System::Net::ICredentials ^ DefaultCredentials { System::Net::ICredentials ^ get(); };
C#
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" );
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");
' 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 通訊協定。
備註
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 |