CredentialCache.DefaultCredentials Properti

Definisi

Mendapatkan kredensial sistem aplikasi.

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

Nilai Properti

ICredentials yang mewakili kredensial sistem aplikasi.

Contoh

Contoh kode berikut menggunakan DefaultCredentials properti untuk mendapatkan kredensial sistem aplikasi.

// 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")

Keterangan

Properti DefaultCredentials hanya berlaku untuk autentikasi berbasis NTLM, negosiasi, dan Kerberos.

DefaultCredentials mewakili kredensial sistem untuk konteks keamanan saat ini di mana aplikasi berjalan. Untuk aplikasi sisi klien, ini biasanya kredensial Windows (nama pengguna, kata sandi, dan domain) pengguna yang menjalankan aplikasi. Untuk aplikasi ASP.NET, kredensial default adalah kredensial pengguna dari pengguna yang masuk, atau pengguna yang ditiru.

Untuk mendapatkan kredensial sebagai NetworkCredential instans, gunakan DefaultNetworkCredentials properti .

Nilai yang didukung untuk authType adalah "NTLM", "Digest", "Kerberos", dan "Negosiasi". Metode ini tidak berfungsi untuk protokol HTTP atau FTP.

Catatan

Instans ICredentials yang dikembalikan oleh DefaultCredentials tidak dapat digunakan untuk melihat nama pengguna, kata sandi, atau domain dari konteks keamanan saat ini.

Berlaku untuk