Monday, August 23, 2010

How to call InternetErrorDlg to deal with certificate issues on SSL connections (C#)

Hi all,
The following C# sample shows how to call WinInet APIs to make an SSL request and deal with possible certificate issues with InternetErrorDlg (which will show the same standard dialogs that Internet Explorer shows when something is wrong with server or client certs):

  1. using
 System;  

  • using System.Collections.Generic;  



  • using System.Text;  



  • using System.Runtime.InteropServices;  



  • using System.Windows.Forms;  



  • using System.Net;  



  •   



  • namespace WindowsApplication1  



  • {  



  •     class Tester  



  •     {  



  •         public static string TestSSLRequest(IntPtr hWnd, string lpszServerName, short nServerPort, string lpszUrl)  



  •         {  



  •             // Variables  



  •             IntPtr hInternet = IntPtr.Zero;  



  •             string lpszAgent;  



  •             int dwAccessType = 0;  



  •             string lpszProxyName;  



  •             string lpszProxyBypass;  



  •             int dwFlags = 0;  



  •             string lpszUserName;  



  •             string lpszPassword;  



  •             int dwService = 0;  



  •             IntPtr dwContext = IntPtr.Zero;  



  •             IntPtr hConnect = IntPtr.Zero;  



  •             string lpszVerb;  



  •             string lpszObjectName;