1

I am using following code to sign the data:

        RSACryptoServiceProvider csp;

        X509Store my = new X509Store(StoreName.My, StoreLocation.LocalMachine);

        my.Open(OpenFlags.ReadOnly);

        foreach (X509Certificate2 cert in my.Certificates)
        {
            if (cert.Subject.Contains(certSubject))
            {
                csp = (RSACryptoServiceProvider)cert.PrivateKey;
            }
        }
        csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA256"));

First time after installing the certificate to my local machine works fine but when I try to sign after the computer restarting it throws an exception. What could be the possible reason, please help.

Aasish
  • 377
  • 5
  • 18
  • Does anywhere in your code set something like `csp.PersistKeyInCsp = false`? If so, it's when that object gets disposed. – bartonjs Feb 10 '17 at 15:26
  • No, Computer without restarting works fine, but after restart problem occurs, Is it disposed when system restart ? – Aasish Feb 11 '17 at 05:04
  • Installing certificate on current user instead of Local machine worked for me, but installing on local machine occurs same error. – Aasish Feb 12 '17 at 05:59

1 Answers1

1

I had the same issue. IIS Express running in my account was able to access the private key of the certificate on the day I was importing it on the local machine but not after a restart. The way I was able to fix was by explicitly grant full rights to my account in the certlm console (even thou my account was already in the local administrators group which had full-rights on that private key).