CredentialCache.DefaultCredentials 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取应用程序的系统凭据。
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
属性值
表示应用程序的系统凭据的 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" );
// 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的 实例不能用于查看当前安全上下文的用户名、密码或域。