This error (m_safeCertContext is an invalid handle.) occurs when you try to access a client cert (or a member of the cert) from a ASP.NET 2.0 application developed using VS2005 eg:
X509Certificate cert = new X509Certificate(Request.ClientCertificate.Certificate);
string certSerial = cert.GetSerialNumberString();
The error is thrown because no valid certificate is found. This can be caused by either:
- A problem with the cert. A common reason for this in a development environment is one of the cert properties differing from that of the environment eg cert common name does not match the site name. I guess this would also occur if the cert had expired.
- The cert did not get passed to the request.
If the cert has not been passed to the request. Then it's probably because you've not setup the website to be able to accept certs. To do this you need to do the following:
- Host the project from IIS - not the thin web server that is bundled with VS2005.
- From within VS2005 select File - New Web Site.
- Select the Location of HTTP then enter the path eg: http://localhost/MyWebSite. Note that you don't have to use HTTPS just yet (I find it easier for development to use HTTP then when deploying to UAT or Production to use HTTPS).
- Code up a test form.
- Go into IIS Admin - right click on the new app (MyWebSite) and select Properties.
- On the Directory Security tab, click Edit... under 'Secure communications'.
- Make sure 'Accept client certificates' is checked.
- When you run your app - make sure you use HTTPS in the url eg: https://localhost/MyWebSite
Here's another article about this.
HTH
Tim