CredentialCache.DefaultCredentials CredentialCache.DefaultCredentials CredentialCache.DefaultCredentials CredentialCache.DefaultCredentials Property

定义

获取应用程序的系统凭据。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

属性值

表示应用程序的系统凭据的 ICredentialsAn 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、 协商和基于 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"、"Digest"、"Kerberos"和"协商"。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.

安全性

EnvironmentPermission
用于读取 USERNAME 的环境变量。for reading the USERNAME environment variable. 关联的枚举: ReadAssociated enumeration: Read

适用于