X509 Certificate error - System cannot find the file specified


(kumar) #1

Facing error in the NSB worker, while trying to Load the X509Certificate2 . The same code works well independently, and as a stand alone application. However, errors out within the NSB structure, either as a worker or as a Test console app. The sample code is as follow -

class Program
    static void Main(string[] args)
            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();

            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)
                String.Format("Exception occurred{0}Message:{1}{2}Inner Exception: {3}", Environment.NewLine, ex.Message, Environment.NewLine, ex.InnerException));

The error is observed 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.

(kumar) #2

After some hit and trials, I have come to a workaround/resolution. The solution is in, providing an Absolute Path, rather than a relative path.
The intent of providing a relative path, was to fetch the certificate path, post deployment, on any given server and path. Basically, include the certificate as part of the artifact, write the same to the output path, and read the contents.
However, with the current errors, the resolution seems to be, in providing a common Absolute path. If anyone has any alternate/efficient resolutions, please suggest.

(Dennis van der Stelt) #3

Hi Rakesh,

I’m not sure what this has to do with NServiceBus? You’re loading a certificate file but you assumed a certain path and placed your certificate in there, but .NET was unable to load it from there? The error clearly says so.

Maybe verify the value of filepath or whatever it is you’re doing in the code running inside your endpoint? I’m sure it’ll work with a relative path, if you put the certificate inside the right folder.