共用方式為


WebRequest.Credentials 屬性

定義

在子代類別中覆寫時,取得或設定用來向因特網資源驗證要求的網路認證。

public:
 virtual property System::Net::ICredentials ^ Credentials { System::Net::ICredentials ^ get(); void set(System::Net::ICredentials ^ value); };
public virtual System.Net.ICredentials Credentials { get; set; }
public virtual System.Net.ICredentials? Credentials { get; set; }
member this.Credentials : System.Net.ICredentials with get, set
Public Overridable Property Credentials As ICredentials

屬性值

ICredentials,其中包含與要求相關聯的驗證認證。 預設值為 null

例外狀況

當屬性未在子系類別中覆寫時,會嘗試取得或設定 屬性。

範例

下列範例會使用目前使用者的預設認證來設定 Credentials 屬性。 提出要求時,儲存在此屬性中的認證會用來驗證用戶端。 這與將 UseDefaultCredentials 屬性設定為 true相同。

// Create a request for the URL.   
WebRequest^ request = WebRequest::Create( "http://www.contoso.com/default.html" );

// If required by the server, set the credentials.
request->Credentials = CredentialCache::DefaultCredentials;

// Get the response.
HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
// Create a request for the URL. 		
WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
' Create a request for the URL. 		
Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
' If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials
' Get the response.
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

Credentials 屬性包含存取因特網資源所需的驗證認證。

注意

WebRequest 類別是 abstract 類別。 運行時間 WebRequest 實例的實際行為是由 WebRequest.Create 方法所傳回的子代類別所決定。 如需預設值和例外狀況的詳細資訊,請參閱子代類別的檔,例如 HttpWebRequestFileWebRequest

適用於

另請參閱