【发布时间】:2017-05-29 15:36:31
【问题描述】:
我正在开发窗口服务,我需要调用使用 x509 证书类型安全性进行身份验证的 Web 服务。
现在我通过导入 .pfx 和 .crt 文件然后导出 .cer 文件在我的系统上生成了 .cer 文件。我在 VS2015 中使用它来与 Web 服务通信。
它工作正常。然后,如果我在同一系统上创建设置并安装,但在下面给出错误。 Web服务返回的错误
"error_name" : "authentication_failure",
"error_advice" : "Your identity could not be authenticated. This may mean that your request was not accompanied with your Client Certificate, or your software does not have access to your Private Key in order to encrypt messages correctly. Please check your software's configuration and associated file permissions for both your Client Certificate and Private Key. In addition, please check that your software supports secure connections using TLS 1.2 or higher."
之前我尝试在 Visual Studio 中调试时使用下面的代码加载 .cer,它工作正常
X509Certificate certificate = X509Certificate.CreateFromCertFile(ConfigurationManager.AppSettings["CertificateFilePath"].ToString());
在 app.config 中
<add key="CertificateFilePath" value="E:\ZooplaCerts\test\zooplasandbox_cert.cer"/>
现在,当我创建设置并安装和运行窗口服务时,我遇到了上述错误
我已尝试将代码更改为以下
var certificate = new X509Certificate2(ConfigurationManager.AppSettings["CertificateFilePath"].ToString(), string.Empty, X509KeyStorageFlags.MachineKeySet);
但仍然出现同样的错误。然后我尝试从服务中更改Log On Type 并选择Local System Account 但仍然是同样的错误
尝试通过安装运行窗口服务时收到错误
"error_name" : "authentication_failure",
"error_advice" : "Your identity could not be authenticated. This may mean that your request was not accompanied with your Client Certificate, or your software does not have access to your Private Key in order to encrypt messages correctly. Please check your software's configuration and associated file permissions for both your Client Certificate and Private Key. In addition, please check that your software supports secure connections using TLS 1.2 or higher."
【问题讨论】:
-
请不要问“为什么我会得到 NullReferenceException”的问题,尤其是在这种情况下,如回溯的第一行所示,代码中明显抛出了错误。答案总是因为某些东西是空的,而您在尝试使用空值之前没有检查它。当您将代码作为服务运行时,请进行一些调试并找出空值。您的问题真的应该是“为什么当我将程序作为服务运行时 XYZ 为空”?
-
您还不能得出“显然是因为窗口服务无法访问该文件”的结论。首先调试应用程序,查看哪个引用为空,然后进一步深入研究。服务应用的运行方式与普通应用完全不同,因此请在猜测原因之前仔细调试。
-
@LexLi 我如何将 Visual Studio 中的服务作为服务进行调试?因为正如我提到的,如果我尝试通过代码运行一切正常,并且我在网上看到通过窗口服务访问时存在 .cer 文件限制
-
你为什么不能问谷歌?像msdn.microsoft.com/en-us/library/7a50syb3(v=vs.110).aspx 这样的文章很容易找到。
-
@LexLi 我将代码作为服务调试并发现错误.. 它没有加载 .cer 文件.. Web 服务通过它返回的错误与此有关。我已经更新了我的问题中的确切错误。
标签: c# ssl-certificate x509certificate wcf-security