Sending Client Certificate in HttpWebRequest
- by Aaron Fischer
I am trying to pass a client certificate to a server using the code below however I still revive the HTTP Error 403.7 - Forbidden: SSL client certificate is required. What are the possible reasons the HttpWebRequest would not send the client certificate?
var clientCertificate = new X509Certificate2( @"C:\Development\TestClient.pfx", "bob" );
HttpWebRequest tRequest = ( HttpWebRequest )WebRequest.Create( "https://ofxtest.com/ofxr.dll" );
tRequest.ClientCertificates.Add( clientCertificate );
tRequest.PreAuthenticate = true;
tRequest.KeepAlive = true;
tRequest.Credentials = CredentialCache.DefaultCredentials;
tRequest.Method = "POST";
var encoder = new ASCIIEncoding();
var requestData = encoder.GetBytes( "<OFX></OFX>" );
tRequest.GetRequestStream().Write( requestData, 0, requestData.Length );
tRequest.GetRequestStream().Close();
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback( CertPolicy.ValidateServerCertificate );
WriteResponse( tRequest.GetResponse() );