WCF 4 - TransportWithMessageCredential 使用 X.509 证书进行传输和消息安全

标签 wcf ssl x509certificate ws-security transport-security

我正在尝试在 IIS 上托管一个 WCF4 服务,该服务将使用 X.509 证书来实现消息安全,并使用 SSL 以及传输安全所需的相互身份验证(项目规范要求使用 WS-Security AND SSL,AFAIK 通常只有一种方法,但没关系)。

我制作了我的 TestRootCA 并用它来颁发服务器证书 (localhost) 和客户端证书 (TestUser)。我首先只想建立和测试传输安全性,因此我将 IIS 配置为使用 https,配置了 SSL 证书(我制作的本地主机证书),并将 IIS 配置为要求来自客户端的客户端证书。然后我修改了服务 web.config,制作了相应的 svcutil.conf 并运行了 svcutil,它成功地为我的测试 WinForms 应用程序制作了客户端代理类和 app.config。我通过调用创建代理客户端时设置证书:

var client = new ServiceClient();
client.ClientCredentials.ClientCertificate.SetCertificate(System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser, System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectDistinguishedName, "CN=TestUser");
MessageBox.Show(client.GetData(42));

到目前为止一切顺利。但是,当我修改服务的 web.config 以使用 TrasportWithMessageCredential(而不是 Transport)并将“Certificate”指定为消息安全的 clientCredentialType 时,我得到 HTTP 403 - The HTTP request was forbidden with client authentication scheme 'Anonymous',即使我指定了“证书”。

我最终的 web.config 如下所示:

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="wsHttpEndpointBinding">
                <security mode="TransportWithMessageCredential">
                    <transport clientCredentialType="Certificate"/>
                    <message clientCredentialType="Certificate"/>
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <services>
        <service behaviorConfiguration="serviceBehavior" name="Service">
            <endpoint binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" name="wsHttpEndpoint" contract="IService" />
            <endpoint address="mex" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" name="mexEndpoint" contract="IMetadataExchange" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="serviceBehavior">
                <serviceMetadata httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

和我的客户端 app.conf:

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="wsHttpEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
                <security mode="TransportWithMessageCredential">
                    <transport clientCredentialType="Certificate"/>
                    <message clientCredentialType="Certificate"/>
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://localhost/WCFTestService/Service.svc" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpoint" contract="IService" name="wsHttpEndpoint" />
    </client>
</system.serviceModel>

有什么想法吗?

编辑:

我已设法通过将客户端证书的 IIS SSL 设置从要求更改为接受来使其正常工作。似乎当我使用证书而不是使用 SSL 的传输安全引入消息安全时,客户端使用证书进行消息签名,但通过 SSL 进行相互身份验证(它尝试以匿名<登录/em> 即使它已分配证书)。

不确定为什么会这样,我想听听专家的意见 :)。

最佳答案

如问题中所述,我通过将客户端证书的 IIS SSL 设置从 require 更改为 accept 使其正常工作。然后在服务入口点,我以编程方式检查远程用户的证书(如果它不为空且有效)。

关于WCF 4 - TransportWithMessageCredential 使用 X.509 证书进行传输和消息安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620810/

相关文章:

使用通配符证书的 WCF 服务给出了 DNS 身份错误

ssl - 如何判断一个网站使用的是CA证书还是SSL证书?

wcf - 使用 WCF 的 Ionic 自定义身份验证端点文档/示例

c# - 反序列化抽象类

java - Jetty Websocket 连接 - 忽略自签名证书

ssl - 寻找有关 "HTTP to HTTPS"的分步指南

wcf - IIS 7 自动重启

.net - 在 WCF 服务中测试异常处理

SSL 证书 : how to setup CA to deal with certificate chain?

c# - 如何在 C# 中为 DSA key 对创建带有自签名证书的 PKCS12 p12 文件?