无法创建SSL/TLS安全通道适用于winforms但不适用于asp.net

Col*_*ech 13 c# asp.net ssl web-services

我有一个Web服务,我通过"添加服务参考"注册,需要HTTPS和证书.下面是我实例化我的服务的代码:

        service = new MyReferencedWebService();

        X509Certificate2 cert = new X509Certificate2();

        var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Mycert.cer");
        var bytes = new byte[stream.Length];

        stream.Read(bytes, 0, bytes.Length);

        cert.Import(bytes, MYPASSWORD, X509KeyStorageFlags.DefaultKeySet);

        service.ClientCredentials.ClientCertificate.Certificate = cert;
Run Code Online (Sandbox Code Playgroud)

我的配置看起来像这样:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="RecordGeneratorWebServiceSoapHttp">
                <security mode="Transport">
                    <transport clientCredentialType="Certificate" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://mywebserviceurl"
            binding="basicHttpBinding" bindingConfiguration="RecordGeneratorWebServiceSoapHttp"
            contract="MyService.RecordGeneratorWebServiceInterface"
            name="RecordGeneratorWebServicePort" />
    </client>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

如果我创建一个简单的winforms .exe并使用上面的代码,我会收到来自我的Web服务的响应.但是,如果我在ASP.NET中使用相同的代码,我会得到以下内容:

请求已中止:无法创建SSL/TLS安全通道.

如何在ASP.NET中完成这项工作?

编辑: 我应该补充.我使用的客户端证书与智能卡绑定,需要输入PIN才能使用.不确定这是否有所不同.

当客户端登录到应用程序时,它会提示他们输入证书PIN.在这种情况下,他们将CAC卡插入CAC阅读器.那么也许我可以以某种方式使用Request.ClientCertificate?

Eve*_*zon 0

证书的根证书是否可访问?如果您自己导入它,它会导入到您的用户的证书存储中,iis 无法访问该证书存储(而是将其导入到计算机存储中)

该类构造函数的文档表明该类将访问 CSP 来存储证书(对于 pfx)私钥。这对于 cer 文件来说不是必需的,但如果您作为应用程序池用户运行,也许您仍然会遇到权限问题?

尝试将应用程序池切换为以具有权限的用户身份运行,看看是否有帮助,或者尝试将证书导入到计算机证书存储中并从那里访问它。