3 posts tagged “x509”
http://www.codeplex.com/wikipage?ProjectName=webserver&title=HTTPS
When was the last time you decided you needed to deploy a product of yours. How often is it that you run into a situation where configuration and the manual to installation of your projects becomes a royal pain in the neck? Well, for most, maybe not all that often, but for the few in the corporate world - this may happen all too often.
using System.Security.Cryptography.X509Certificates;
namespace AddCert
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 7 && args.Length != 5)
Console.WriteLine("Usage: addcert cert.pfx /r LocalMachine /s My /pass mypass");
string path = args[0];
StoreLocation sl = (StoreLocation)Enum.Parse(typeof(StoreLocation), args[2]);
StoreName sn = (StoreName)Enum.Parse(typeof(StoreName), args[4]);
X509Store store = new X509Store(sn, sl);
store.Open(OpenFlags.ReadWrite);
if (args.Length == 5)
store.Add(new X509Certificate2(path));
else if (args.Length == 7)
store.Add(new X509Certificate2(path, args[6]));
store.Close();
}
}
}
A thousand times over, I will explain this one seemingly simple concept. Whenever you feel like you are running into a keyset does not exist issue. There are several plausible causes to this.


- Make sure that the certificate you are looking for is actually in the certificate store. You can see above that I am in the Local Machine certificate store. If you are attempting to load a certificate from the LocalMachine Personal Store then it should be located there.
- Does the certificate have a private key? Every certificate is associated with a private key, but it may not actually be available with the certificate itself. And, depending on how you imported the certificate, many times the private key may have been imported under your user account - but if you by chance copied a certificate into a different store, the private key may not go with it. It's important that you import the certificates to the right place the first time.
- Does your process have sufficient rights to access the certificate? With a web-application you will need to grant the web app access through a tool called winhttpcertcfg.exe. Otherwise, you will probably need to run the process under the same account that the certificate is running with. System is sufficient for a certificate in the Local Machine. For developing in Vista and above, simply run Visual Studio in Administrator mode and you should have access to it.