gpt4 book ai didi

c# - X509Certificate2 错误 - 系统找不到指定的文件

转载 作者:太空宇宙 更新时间:2023-11-03 18:21:43 27 4
gpt4 key购买 nike

关联的代码在独立的控制台应用程序中运行良好,而在 NSB 架构中尝试使其运行时出错。我试图在工作人员中测试相同的内容,也在测试控制台应用程序中独立测试。无论哪种情况,它都会在行中出错 - X509Certificate2 certificate = new X509Certificate2(filePath, "***key***UeUHFxS");异常消息是 -System.Security.Cryptography.CryptographicException: '系统找不到指定的文件。该代码由所示代码和激活设备的相关帮助程序文件组成。但是,在用于从 pfx 文件路径和 key 初始化 X509Certificate2 的部分中存在异常(exception)情况。

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));
}

}

}

最佳答案

让我们尝试将您的构造函数修改为:

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

使用 MachineKeySet 作为 msdn 说:

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

关于c# - X509Certificate2 错误 - 系统找不到指定的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51490379/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com