Export (0) Print
Expand All

CredentialCache.DefaultCredentials Property

Gets the system credentials of the application.

[Visual Basic]
Public Shared ReadOnly Property DefaultCredentials As ICredentials
[C#]
public static ICredentials DefaultCredentials {get;}
[C++]
public: __property static ICredentials* get_DefaultCredentials();
[JScript]
public static function get DefaultCredentials() : ICredentials;

Property Value

An ICredentials that represents the system credentials of the application.

Remarks

The DefaultCredentials property applies only to NTLM, negotiate, and Kerberos-based authentication.

DefaultCredentials represents the system credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application. For ASP.NET applications, the default credentials are the user credentials of the logged-in user, or the user being impersonated.

Note   The ICredentials instance returned by DefaultCredentials cannot be used to view the user name, password, or domain of the current security context.

Example

[Visual Basic, C#, C++] The following example uses the DefaultCredentials method to get the system credentials of the application.

[Visual Basic] 
' 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")

[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");

[C++] 
// Ensure Directory Security settings for default web site in IIS is S"Windows Authentication".
String* url = S"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(S"Authentication successful");
Console::WriteLine(S"Response received successfully");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

CredentialCache Class | CredentialCache Members | System.Net Namespace

Show:
© 2014 Microsoft