m_safeCertContext 是一个无效的句柄 - 为什么我的代码在生产环境中工作而不在开发环境中工作?

时间:2021-04-06 16:01:11

标签: visual-studio ide iis-express

在我的项目中启用 HTTPS 后,我开始收到带有此错误的错误页面:library(dplyr) df %>% group_by(State) %>% mutate(Diff = c(NA, diff(GDP)))

代码在 Web 服务器的生产环境中运行良好。

我正在运行 Visual Studio 2017 15.3.0。

我已将 sslFlags 更新为 m_safeCertContext is an invalid handle 我已经确保 iisClientCertificateMappingAuthentication 已设置 到<access sslFlags="SslNegotiateCert" />

以下是我的 IIS Express 应用程序池设置:

<iisClientCertificateMappingAuthentication enabled="true"></iisClientCertificateMappingAuthentication>

错误页面的全文:

<applicationPools>
            <add name="Clr4IntegratedAppPool" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated" CLRConfigFile="%IIS_USER_HOME%\config\aspnet.config" autoStart="true" />
            <add name="Clr4ClassicAppPool" managedRuntimeVersion="v4.0" managedPipelineMode="Classic" CLRConfigFile="%IIS_USER_HOME%\config\aspnet.config" autoStart="true" />
            <add name="Clr2IntegratedAppPool" managedRuntimeVersion="v2.0" managedPipelineMode="Integrated" CLRConfigFile="%IIS_USER_HOME%\config\aspnet.config" autoStart="true" />
            <add name="Clr2ClassicAppPool" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" CLRConfigFile="%IIS_USER_HOME%\config\aspnet.config" autoStart="true" />
            <add name="UnmanagedClassicAppPool" managedRuntimeVersion="" managedPipelineMode="Classic" autoStart="true" />
            <applicationPoolDefaults managedRuntimeLoader="v4.0">
                <processModel loadUserProfile="true" />                                
            </applicationPoolDefaults>
</applicationPools>

2 个答案:

答案 0 :(得分:1)

调用堆栈至少给出了关于下一步要查找的内容的足够提示。

Line 383:                    HttpClientCertificate httpCert = HttpContext.Current.Request.ClientCertificate;
Line 384:                    X509Certificate2 x509Cert = new X509Certificate2(httpCert.Certificate);
Line 385:                    _subject = x509Cert.Subject;

这段代码试图获取浏览器发送的客户端证书,然后访问其 Subject 属性。此访问尝试失败并发生异常,

[CryptographicException: m_safeCertContext is an invalid handle.]
   System.Security.Cryptography.X509Certificates.X509Certificate.ThrowIfContextInvalid() +12505161
   System.Security.Cryptography.X509Certificates.X509Certificate.get_Subject() +14

由于 .NET Framework 通常包含本地 Windows API,因此异常本身可能会非常令人困惑。

“代码在 web 服务器上的生产环境中运行良好”,因此我可以假设您的服务器管理员已经配置了这台生产机器,知道如何为客户端证书构建完整链(非常常见的服务器配置),但您可能不知道在你的开发机器上做了同样的事情。与服务器管理员交谈,了解您错过了什么。

同时,您可以在第 384 行设置中断并分析 httpCert 的更多详细信息。这可能会帮助您找出缺少哪些设置。

答案 1 :(得分:0)

我更新了错误的 ApplicationHost.config 文件。

正确的位于解决方案根目录中的 .vs\config

我正在更新位于:C:\Users\[username]\Documents\IISExpress\config\

一旦我更新了正确的 ApplicationHost.config 文件,代码就开始在我的本地开发环境中工作。

相关问题