我在我的开发机器上有一个需要证书的WCF客户端,它工作正常.
部署到生产服务器后,我收到以下错误:
[CryptographicException: The specified network password is not correct.]
Run Code Online (Sandbox Code Playgroud)
DEV - Win7 32BIT IIS 7.5
生产 - Win SERVER 64BIT 2008 IIS 7.5
即使网络之间没有密码也没有证书密码.(我知道因为开发工作没有密码).我唯一的密码是WCF,它与DEV相同.
CrmServiceClient crm = new CrmServiceClient("CrmServiceEndpoint");
crm.ClientCredentials.UserName.UserName = CrmConfigRepository.CrmUserName;//fine
crm.ClientCredentials.UserName.Password = CrmConfigRepository.CrmPassword;//fine
crm.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(Path);
///THIS WONT WORK AS WELL
crm.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(Path, "", X509KeyStorageFlags.Exportable);
Run Code Online (Sandbox Code Playgroud)
这是完整的堆栈
[CryptographicException: The specified network password is not correct. ]
System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +41
System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) +0
System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags …Run Code Online (Sandbox Code Playgroud) 我有一个控制台应用程序,它从字节数组中加载X509证书,如下所示:
var cert = new X509Certificate2(certificateContent, // byte[]
password, // string
X509KeyStorageFlags.PersistKeySet);
Run Code Online (Sandbox Code Playgroud)
certificateContent是byte[]表示pfx文件的内容。该代码对我测试过的许多证书都适用。但是,我正在测试一个证书,该证书导致此行抛出一条CryptographicException错误消息“指定的网络密码不正确。”,即使提供的密码正确无误。
奇怪的是,我可以在LinqPad中使用相同的代码从具有相同密码的相同pfx文件创建证书,并且可以正常工作。
我已经在调试器的控制台应用程序中检查了呼叫站点,并验证了是否传递了正确的值。
是什么导致该构造函数在控制台应用程序中抛出此异常,但在使用相同数据的LinqPad中却未抛出此异常,并且对于其他证书在两个地方均能正常工作?
更多细节
证书存储在Base64的数据库中。Console应用程序从数据库读取证书,将其从Base64转换为byte [],然后尝试X509Certificate2如上所述创建对象。
我一直在测试三个证书:
证书1和2在控制台应用程序和LinqPad中均按预期工作。
证书3在LinqPad中可以很好地加载,但是如果我尝试在控制台应用程序中使用它,则会生成上面的错误。
证书2和3之间有两个重要区别。
这些差异之一是否会导致“指定的网络密码不正确”错误?为何所有3个证书在LinqPad中都能正常工作,而在Console应用程序中只有1个抛出错误?