WCF客户端证书和用户名凭据被禁止

时间:2013-08-15 07:14:11

标签: c# wcf security client-certificates

我在使用WCF客户端连接到需要客户端证书和用户名/密码的服务器时遇到问题。

我已经确认使用JUST客户端证书可以使用:

        var binding = new WSHttpBinding(SecurityMode.Transport);
        binding.Security.Transport = new HttpTransportSecurity();
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

        var ea = new EndpointAddress(EndpointUrl);

        using (var p = new ServiceReference1.AAAClient(binding, ea))
        {
            try
            {
                p.ClientCredentials.ClientCertificate.SetCertificate(
                        System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser,
                        System.Security.Cryptography.X509Certificates.StoreName.My,
                        System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName,
                        CertificateSubject);

                var x = p.RequiresClientCertificateOnly();
                Console.WriteLine("Status: " + x.status);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine();
        }

下一个合乎逻辑的步骤是添加用户名/密码部分,但它会在未经授权的情况下返回403。我知道凭证和证书是有效的,因为我在中间使用fiddler提供客户端证书和用户名/密码,它工作正常,只是当通过WCF使用它似乎无法工作,或使用发送两个客户端证书和用户名/密码。

尝试使用两者的代码如下(尝试使用WSHttpBinding和BasicHttpBinding - 两者的结果相同):

        var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
        binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

        var ea = new EndpointAddress(EndpointUrl);

        using (var p = new ServiceReference1.PingPortTypeClient(binding, ea))
        {
            try
            {
                p.ClientCredentials.ClientCertificate.SetCertificate(
                        System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser,
                        System.Security.Cryptography.X509Certificates.StoreName.My,
                        System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName,
                        CertificateSubject);

                p.ClientCredentials.UserName.UserName = Username;
                p.ClientCredentials.UserName.Password = Password;

                var x = p.pingDatabase();
                Console.WriteLine("Status: " + x.status);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine();

当我直接在浏览器中转到URL时 - 我收到错误:

  

HTTP错误403.7 - 禁止您尝试访问的页面   要求您的浏览器具有安全套接字层(SSL)客户端   Web服务器识别的证书。

这表明在上面的第二个例子中没有发送证书,因为收到了相同的错误(403)。

有人可以帮我配置WCF以提供客户端证书和用户名/密码吗?我已经在网上搜索了,这里的“类似问题”根本没有帮助。现在变得非常沮丧 - 我错过了什么?

2 个答案:

答案 0 :(得分:5)

如果您需要用户名(消息级别)和证书(传输级别),则必须使用自定义绑定。例如:

        <customBinding>
            <binding name="NewBinding0">
                <textMessageEncoding messageVersion="Default" />
                <security authenticationMode="UserNameOverTransport">
                    <secureConversationBootstrap />
                </security>
                <httpsTransport requireClientCertificate="true" />
            </binding>
        </customBinding>

messageVersion的值取决于您是否要使用WSHttp或BasicHttp。目前我已将其设置为“默认”,即WSHttp,对于Basic,将其设置为“Soap11”。

答案 1 :(得分:0)

用户名和证书都在传输级别

不是该问题的答案,但问题标题将我带到了这里,所以也许其他人也是如此:
我需要一个Soap11绑定,同时具有证书(用于网关)和传输级别的用户名(用于应用程序)。 (这两个部分都是由客户现场的不同团队和不同供应商维护的。)

尽管名称customBindingUserNameOverTransport会在Soap标头(消息级)而不是http标头(传输级)中发送用户名和密码,从而导致内部{{ FaultException中的1}},源自应用程序:

SecurityRequestChannel.ProcessReply

FaultException: MustUnderstand headers: [{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}Security] are not understood. basicHttpBinding结合使用会导致:

<security mode="TransportCredentialOnly">

经过 次反复试验后,事实证明,可以通过组合配置和代码来完成此操作。

通过配置传输证书的示例

The provided URI scheme 'https' is invalid; expected 'http'.
Parameter name: via

(我使用此answer动态加载了它。)

通过代码传输的用户名和密码示例

  <basicHttpBinding>
    <binding name="myBasicHttpBindingForTransportCertificate">
      <security mode="Transport">
        <transport clientCredentialType="Certificate" />
      </security>
    </binding>
  </basicHttpBinding>

  <endpointBehaviors>
    <behavior name="myCertificateBehavior">
      <clientCredentials>
        <clientCertificate findValue="my certificate subject" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My" />
      </clientCredentials>
    </behavior>

(感谢以前的answer使用“ WCF TransportCredentialOnly不发送用户名和密码”。)

相关问题