Connect to web site using SSL & Client Certificates


K

Krishna

Well, I got it working when running against my test server (IIS5 W2K
svr, I will attach a sample of the code at the bottom of this
message), but now I'm connecting to our client (Apache) I'm getting
the following error:

An unhandled exception of type 'System.Net.WebException' occurred in
system.dll

Additional information: The underlying connection was closed: Unable
to connect to the remote server.


This exception is thrown when the GetRequestStream() method is
called and I am using a client cert that was issued to us from their
internal CA (they are using OpenSSL).

Any help'd be much appreciated!

Regards,kg.


public void Connect()
{
// Doesn't work
X509Certificate cert =
X509Certificate.CreateFromCertFile("c:\\Certificates\\client_cert.cer");
WebRequest request = GetWebRequest(new
Uri("https://client_web/methodserver.php"));

// Works
//X509Certificate cert =
X509Certificate.CreateFromCertFile("c:\\Certificates\\verisign_cert.cer");
//WebRequest request = GetWebRequest(new
Uri("https://server.com/testpage.aspx"));


request.ContentType = "text/xml";
request.Method = "POST";

HttpWebRequest httpRequest = (HttpWebRequest)request;
httpRequest.UserAgent = "Exel Test";
httpRequest.Headers = new WebHeaderCollection();
httpRequest.ClientCertificates.Add(cert);

request = (WebRequest)httpRequest;

// This is where it breaks...
Stream reqStream = request.GetRequestStream();
reqStream.Close();

WebResponse resp = GetWebResponse(request);
Stream respStream = resp.GetResponseStream();

TextReader tr = new StreamReader(respStream, new UTF8Encoding(), true,
4096);

string certInfo = tr.ReadToEnd();

System.Diagnostics.Debug.WriteLine(certInfo);
}
 
Ad

Advertisements

K

Krishna

Okey dokey, well only a little embarrassed! I didn't have the
private key for the Client Cert that our client gave us, therefor it
wasn't properly loaded in my Personal Cert store, therefor when the
connection was being negotiated it crashed... I got the full key
pair from them and voila!

Hope this helps anyone!

Krishna.
 

Top