Export (0) Print
Expand All

CredentialCache.DefaultCredentials Property

Gets the system credentials of the application.

Namespace: System.Net
Assembly: System (in system.dll)

public static ICredentials DefaultCredentials { get; }
/** @property */
public static ICredentials get_DefaultCredentials ()

public static function get DefaultCredentials () : ICredentials

Not applicable.

Property Value

An ICredentials that represents the system credentials of the application.

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.

To get the credentials as a NetworkCredential instance, use the DefaultNetworkCredentials property.

The supported values for authType are "NTLM", "Digest", "Kerberos", and "Negotiate". This method does not work for HTTP or FTP protocols.

NoteNote:

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

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 = (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");

// 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.set_Credentials(CredentialCache.
    get_DefaultCredentials());

// Send the 'HttpWebRequest' and wait for response.            
HttpWebResponse myHttpWebResponse 
    = (HttpWebResponse)myHttpWebRequest.GetResponse();

Console.WriteLine("Authentication successful");
Console.WriteLine("Response received successfully");

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft