c# - 将 X.509 证书发送到 WCF c#

标签 c# web-services wcf

我有一个使用此类行为的 WCF 服务:

  <behavior name="WCFServiceCertificate.Service1Behavior">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
      <serviceCredentials>
        <clientCertificate>
          <authentication certificateValidationMode="ChainTrust"/>
        </clientCertificate>
        <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
      </serviceCredentials>
    </behavior>

它使用我通过 makecert 创建的名为“localhost”的证书。首先,我创建了根证书颁发机构,然后创建了一个证书。我还生成了一个保存在文件中的客户端证书。

然后,我有一个使用该 Web 服务的客户端应用程序。 App.config 包括:

 <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IWS">
                <security>
                    <message clientCredentialType="Certificate" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>

然后我从文件中为客户端加载证书:

  X509Certificate2 client = new X509Certificate2("client.pfx", "pass");

所有证书的东西似乎都正确完成,但是当我想从客户端调用任何服务方法时,它说:

The caller was not authenticated by the service

有人可以给我一些关于如何将 SOAP header 中的证书从客户端正确传递到服务器的建议吗?我想念什么?

最佳答案

使用证书不是一件容易的事,特别是在 WCF 中调试它。因此,您可以考虑采取以下步骤来缩小问题范围。

  1. 嵌入证书后,您的服务是否运行?测试它是否正常工作的一种方法是尝试在浏览器中访问您的服务。

    例。 http://localhost/Path/Service.svc

  2. 您访问客户端中的服务地址是否与 CN=localhost 证书中描述的相同?

    例。 http://location/Path/Service.svc

    不是这个:

    例。 http://computername/Path/Service.svc

    或者

    例。 http://computername.domain.com/Path/Service.svc

    解释:

    如果证书的 Common Name 中的名称与颁发给该证书的 URL 名称不同,则证书将无效。

  3. 你们在同一台机器上吗? CN=localhost 的证书只能在同一台机器上使用。

  4. 由于您在 IIS 中托管您的服务,因此您需要配置 IIS 以使用您创建的证书并将其绑定(bind)到端口 443。

    如何?

    1. 转到您的 IIS 和客户端默认网站
    2. 点击右侧的绑定(bind)位置
    3. 如果协议(protocol) HTTPS 已经配置,请尝试编辑它并分配您的证书。如果不是,请使用端口 443 添加协议(protocol) HTTPS 并分配您的证书。如果您在商店中正确安装,证书应该会显示在列表中。

通过这些步骤,我们可以知道从哪里开始。可能是服务问题、客户端问题或 IIS 配置问题。希望这能帮助我们解决您的问题。

关于c# - 将 X.509 证书发送到 WCF c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31824490/

相关文章:

c# - SignalR 在最终代理调用时失败

c# - 数据访问层的设计模式

image - 如何通过 WCF 传输图像文件?

WCF : Could not establish trust relationship for the SSL/TLS secure channel for localhost

c# - 从 C# 应用程序运行 PowerShell 脚本

javascript - Web API 发布参数

web-services - 是否必须注册 Web 服务?

java - Facebook Java 服务器

使用 Apt 的 Java Web 服务。我需要注释处理器吗?怎么了?

c# - 为什么在 WCF 中使用 LocalChannel 时执行序列化?