Really hoping you can help me with the following.
I have created an .Net Core Application that is run as a windows service. I'm trying to open a certificate in order to decrypt the app settings.
The certificate I am using on my local machine and the test server is the same certificate. The Public key for the certificate in question is an X5092.
I have the following code to read the Certificate from the store to get the values.
public void Start()
{
using (var store = new X509Store(Location))
{
store.Open(OpenFlags.ReadOnly);
var signingCert = store.Certificates.Find(CriteriaType, _thumb, false);
_logger.Warning($"Number of certs found {signingCert.Count}");
var firstCert = signingCert[0];
_logger.Warning($"Subject - {firstCert.Subject}");
Thread.Sleep(1000);
try
{
_logger.Warning($"Private Key - {firstCert.PrivateKey}");
_logger.Warning($"Get RSA Private Key Method {firstCert.GetRSAPrivateKey()}");
}
catch (Exception ex)
{
_logger.Error(ex.ToString());
}
}
}
The application finds the certificate using the ThumbPrint enum, the thumbprint and doesn't care if the certificate is valid or not. The value of firstCert is the cert I am after. The "Subject" prints to the log file correctly as well as any of the other props apart from the Public and Private Key props.
My application crashes as soon as I call any of the following
firstCert.PrivateKey
firstCert.GetRSAPrivateKey()
firstCert.PublicKey.{anotherProp}
The crashes happen under the following conditions.
- The server is rebooting.
- The application is set to automatic start up.
- The application is run as a service.
- The Logon is Local System Account. When the application does crash, there are no logs after I try to access key data of the cert. No exception caught, the application fails to start, I only find this out by looking at the services to which is says stopped.
The application works correctly in the following conditions
- When debugging on my local machine.
- Running as a service, on automatic start, as the Local System Account on my local machine.
- As above but started from the services feature.
- On the test server but with a delayed start up.
- On the test server but started from the services feature.
The app written is .Net5 My local machine is windows 10 enterprise. The server is 2012 R2.