Export (0) Print
Expand All

ServicePointManager.CertificatePolicy Property

Gets or sets policy for server certificates.

[Visual Basic]
Public Shared Property CertificatePolicy As ICertificatePolicy
[C#]
public static ICertificatePolicy CertificatePolicy {get; set;}
[C++]
public: __property static ICertificatePolicy* get_CertificatePolicy();
public: __property static void set_CertificatePolicy(ICertificatePolicy*);
[JScript]
public static function get CertificatePolicy() : ICertificatePolicy;
public static function set CertificatePolicy(ICertificatePolicy);

Property Value

An instance implementing the ICertificatePolicy interface.

Remarks

When the CertificatePolicy property is set to an ICertificatePolicy interface instance, the ServicePointManager uses the certificate policy defined in that instance instead of the default certificate policy.

The default certificate policy allows valid certificates, as well as valid certificates that have expired.

Example

[Visual Basic, C#, C++] The following example shows how to catch a certificate policy exception for a custom certificate policy. It assumes that the certificate policy object has been defined, that the URI for the Web resource is contained in the variable myUri, and that there is a method named ProcessResponse that performs the work of the application.

[Visual Basic] 
ServicePointManager.CertificatePolicy = New MyCertificatePolicy()
       
' Create the request and receive the response
Try
    Dim myRequest As WebRequest = WebRequest.Create(myUri)
    Dim myResponse As WebResponse = myRequest.GetResponse()
    
    ProcessResponse(myResponse)

    myResponse.Close()
    
' Catch any exceptions
Catch e As WebException
    If e.Status = WebExceptionStatus.TrustFailure Then
        ' Code for handling security certificate problems goes here.
    End If
    ' Other exception handling goes here
 End Try


[C#] 
ServicePointManager.CertificatePolicy = new MyCertificatePolicy();
 
       // Create the request and receive the response
       try
       {
       WebRequest myRequest = WebRequest.Create(myUri);
       WebResponse myResponse = myRequest.GetResponse();
       ProcessResponse(myResponse);
       myResponse.Close();
       }
       // Catch any exceptions
       catch(WebException e)
       {
       if (e.Status == WebExceptionStatus.TrustFailure)
       {
       // Code for handling security certificate problems goes here.
       }
       // Other exception handling goes here
       }
   

[C++] 
ServicePointManager::CertificatePolicy = new MyCertificatePolicy();
 
       // Create the request and receive the response
       try
       {
       WebRequest* myRequest = WebRequest::Create(myUri);
       WebResponse* myResponse = myRequest->GetResponse();
       ProcessResponse(myResponse);
       myResponse->Close();
       }
       // Catch any exceptions
       catch(WebException* e)
       {
       if (e->Status == WebExceptionStatus::TrustFailure)
       {
       // Code for handling security certificate problems goes here.
       }
       // Other exception handling goes here
       }
   

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

.NET Framework Security: 

See Also

ServicePointManager Class | ServicePointManager Members | System.Net Namespace

Show:
© 2014 Microsoft