WebRequest.Credentials WebRequest.Credentials WebRequest.Credentials WebRequest.Credentials Property

定义

当在子类中被重写时,获取或设置用于对 Internet 资源请求进行身份验证的网络凭据。When overridden in a descendant class, gets or sets the network credentials used for authenticating the request with the Internet resource.

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; }
member this.Credentials : System.Net.ICredentials with get, set
Public Overridable Property Credentials As ICredentials

属性值

包含与该请求关联的身份验证凭据的 ICredentialsAn ICredentials containing the authentication credentials associated with the request. 默认值为 nullThe default is null.

异常

当未在子类中重写该属性时,试图获取或设置该属性。Any attempt is made to get or set the property, when the property is not overridden in a descendant class.

示例

下面的示例使用当前Credentials用户的默认凭据设置属性。The following example sets the Credentials property using the default credentials of the current user. 发出请求时,将使用存储在此属性中的凭据来验证客户端。When the request is made, credentials stored in this property are used to validate the client. 这等同于将UseDefaultCredentials属性设置为。 trueThis is identical to setting the UseDefaultCredentials property to 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)

注解

Credentials属性包含访问 Internet 资源所需的身份验证凭据。The Credentials property contains the authentication credentials required to access the Internet resource.

备注

类是一个abstract类。 WebRequestThe WebRequest class is an abstract class. WebRequest实例在运行时的实际行为由WebRequest.Create方法返回的子代类确定。The actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. 有关默认值和异常的详细信息,请参阅子代类的文档,例如HttpWebRequest和。 FileWebRequestFor more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

适用于

另请参阅