I am using the Microsoft.Web.Services.Security and System.Security.Cryptography namespaces to access the certificate store and extract a private key from a certificate that I can use for decryption and digital signing.
The problem is that when I request the key a modal dialog box with the caption 'Exporting your private exchange key' is displayed stating that 'An application is requesting access to a protected item.' It states that the protected item is a 'CryptoAPI Private Key.' If the user clicks the OK button then I can use the private key, otherwise a System.Security.Cryptography.CryptographicExceptio n exception is thrown.
I can understand why this dialog box is shown and why I can not access the private key until the user allows me to do so, however I was wondering if there was a way the user could associate their certificate with my application so that they are not requested to allow access to the key every time the app starts. I do not mind the box being displayed the first time they use the certificate however I think it will annoy my user base if they have to click OK every time they start the app.
Here is a simplified example of my code:
Code:
Imports System.Security.Cryptography
Imports Microsoft.Web.Services.Security.X509
Module Module1
Sub Main()
Dim store As X509CertificateStore
Dim cert As X509Certificate
Dim privateKey As New RSACryptoServiceProvider
store = X509CertificateStore.LocalMachineStore("My")
If store.OpenRead() Then
cert = store.Certificates(0)
privateKey.FromXmlString(cert.Key.ToXmlString(True))
'Do some fancy encryption stuff with the private key...
End If
End Sub
End Module
Note that this code will only work if you add a reference to the Microsoft.Web.Services.dll and have at least one certificate with a private key in it within the Personal section of the Local Computer certificate store.
Any help here would be greatly appreciated.
Regards
Owain Williams