C# – X509Certificate2 Error – System cannot find the file specified

asp.netcnservicebusx509x509certificate2

The associated code, works well in an independent console application, whereas errors out, while trying to make it work, within an NSB architecture. I have tried to test the same within the worker, and also independently within a test console app. In either case, it errors out in the line – X509Certificate2 certificate = new X509Certificate2(filePath, "***key***UeUHFxS");
The exception message being –
System.Security.Cryptography.CryptographicException: 'The system cannot find the file specified.
The code consists of the one as shown and also an associated helper file for the Activate device. The exception is however, in the section for initializing the X509Certificate2, from the pfx file path and key.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            string filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);
            filePath = Directory.GetParent(Directory.GetParent(filePath).FullName).FullName;
            filePath = Path.Combine(filePath, @"Cert\TestCompany-qa.partner.client.siriusxm.com.pfx");

            X509Certificate2 certificate = new X509Certificate2(filePath, "****key****");
            SoapMessageHelper soapHelper = new SoapMessageHelper(certificate, @"https://api-ext-test.siriusxm.com/SAT/UpdateDeviceSatRefresh/v_1");
            var test = soapHelper.ActivateDevice(new ActivateDeviceRequest()
            {
                SourceName = "12493",
                ESN = "W26890HW",
                TimeStamp = DateTime.UtcNow,
                TrasanctionId = System.Guid.NewGuid().ToString()
            });

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(test);

            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                foreach (XmlNode locNode in node)
                {
                    if (locNode.Name == "ns0:responseRefreshDevice")
                    {
                        string resultCode = locNode["ns0:resultCode"].InnerText;
                        string errorCode = locNode["ns0:errorCode"].InnerText;
                        string errorMessage = locNode["ns0:errorMessage"].InnerText;
                        Console.WriteLine(resultCode + errorCode + errorMessage);
                    }
                }
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(
                String.Format("Exception occurred{0}Message:{1}{2}Inner Exception: {3}", Environment.NewLine, ex.Message, Environment.NewLine, ex.InnerException));
        }

    }

}

Best Answer

Let's try to modify your constructor to:

X509Certificate2 certificate = new X509Certificate2(filePath, key, 
                               X509KeyStorageFlags.MachineKeySet
                             | X509KeyStorageFlags.PersistKeySet
                             | X509KeyStorageFlags.Exportable);

Using MachineKeySet as msdn said that:

"Private keys are stored in the local computer store rather than the current user store. "