简体   繁体   English

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

[英]WCF Client Certificate AND UserName Credentials forbidden

I'm having issues getting a WCF client to connect to a server that requires a client certificate and also a username / password.我在让 WCF 客户端连接到需要客户端证书和用户名/密码的服务器时遇到问题。

I have verified that using JUST the client certificate works using this:我已经验证使用 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();
        }

The next logical step was to add the username / password parts, but it comes back with 403 unauthorised.下一个合乎逻辑的步骤是添加用户名/密码部分,但它返回 403 未经授权。 I know the credentials and the certificate are valid as I have used fiddler in the middle to provide the client certificate and username / password and it works fine, just when using all through WCF it doesnt seem to work, or use send both the client certificate and the username / password.我知道凭据和证书是有效的,因为我在中间使用了 fiddler 来提供客户端证书和用户名/密码,并且它工作正常,只是通过 WCF 使用它似乎不起作用,或者同时使用发送客户端证书和用户名/密码。

The code trying to use both is below (tried with WSHttpBinding and BasicHttpBinding - same result for both):尝试使用两者的代码如下(尝试使用 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();

When I goto the URL directly in a browser - I get the error:当我直接在浏览器中转到 URL 时 - 我收到错误消息:

HTTP Error 403.7 - Forbidden The page you are attempting to access requires your browser to have a Secure Sockets Layer (SSL) client certificate that the Web server recognizes. HTTP 错误 403.7 - 禁止 您尝试访问的页面要求您的浏览器具有 Web 服务器可识别的安全套接字层 (SSL) 客户端证书。

This to suggests that the certificate is not being sent in the second example above as it is the same error (403) being received.这表明在上面的第二个示例中没有发送证书,因为它收到了相同的错误 (403)。

Can someone please help me with the configuration for WCF to provide the client certificate AND the username / password?有人可以帮我配置 WCF 以提供客户端证书和用户名/密码吗? I have trawled the web, and the 'similar questions' on here have not helped at all.我浏览了网络,这里的“类似问题”根本没有帮助。 Getting very frustrate by it now - what am i missing?现在变得非常沮丧 - 我错过了什么?

You have to use a custom binding if you want both username (message level) and certificate (transport level).如果您需要用户名(消息级别)和证书(传输级别),则必须使用自定义绑定。 For example:例如:

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

The value of messageVersion depends if you want to use WSHttp or BasicHttp. messageVersion 的值取决于您是要使用 WSHttp 还是 BasicHttp。 Currently I've set it to "Default" which is WSHttp, for Basic set it to "Soap11".目前我已将其设置为“默认”,即 WSHttp,对于 Basic,将其设置为“Soap11”。

Username and certificate both on transport level传输级别的用户名和证书

Not an answer to this question, but the question title brought me here, so maybe others also:不是这个问题的答案,但问题标题把我带到了这里,所以也许其他人也有:
I needed a Soap11-binding with both certificate (for the gateway) and username on transport level (for the application).我需要一个 Soap11 绑定,同时具有证书(对于网关)和传输级别(对于应用程序)的用户名。 (Both parts were maintained by different teams and different suppliers at the customer site.) (这两个部件都由客户现场的不同团队和不同供应商维护。)

Despite the name a customBinding with UserNameOverTransport sends the username&password in the Soap-header (message-level), not in the http-header (transport-level), leading to this inner FaultException in SecurityRequestChannel.ProcessReply , originating from the application:尽管名称为带有UserNameOverTransportcustomBinding在 Soap-header(消息级别)中发送用户名和密码,而不是在 http-header(传输级别)中,导致SecurityRequestChannel.ProcessReply此内部FaultException源自应用程序:

FaultException: MustUnderstand headers: [{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}Security] are not understood.

And using basicHttpBinding with <security mode="TransportCredentialOnly"> leads to:basicHttpBinding<security mode="TransportCredentialOnly">会导致:

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

After a lot of trial and error, it turned out this can be done by combining configuration and code.经过大量的反复试验,结果证明这可以通过组合配置和代码来完成。

Example of certificate over transport by configuration通过配置传输的证书示例

  <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>

(I dynamically loaded it, using this answer .) (我使用这个答案动态加载了它。)

Example of username&password over transport by code通过代码传输的用户名和密码示例

using (new OperationContextScope(client.InnerChannel))
{
    …
    var httpRequestProperty = new HttpRequestMessageProperty();
    httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] =
           "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}"));
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

(Thanks to this old answer to "WCF TransportCredentialOnly not sending username and password".) (感谢这个对“WCF TransportCredentialOnly 不发送用户名和密码”的旧回答。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何配置 wcf 服务客户端以在消息级别同时使用证书和用户名凭据? - How to configure a wcf service client to use both certificate and username credentials at the message level? 客户端在WCF中是否需要用于用户名身份验证的证书 - Does the client need a certificate for username authentication in WCF 带有用户名令牌和客户端证书的WCF SOAP调用 - WCF SOAP call with both username token and client certificate 如何在WCF客户端中提供UserName和Client Certificate(为什么这个例子有用)? - How to supply both UserName and Client Certificate in WCF client (why does this example work)? WCF和客户端证书身份验证 - WCF and client certificate authentication 如何在WCF中使用自定义绑定并使用用户名客户端凭据保持消息安全模式? - How to use custom binding in WCF and keep message security mode with username client credentials? 如何将用户名/密码凭据从php客户端传递到自托管的wcf服务? - How do I pass username/password credentials from php client to self-hosted wcf service? 结合使用Certificate和UserName在WCF中通过basicHttpsBinding进行客户端身份验证以传递反向代理身份验证 - Use both Certificate and UserName for client authentication in WCF with basicHttpsBinding for passing reverse proxy authentication WCF 下的 SOAP web 服务的同时客户端证书和用户名身份验证 - Simultaneous client-certificate and username authentication of a SOAP web service under WCF WCF:偶尔拒绝客户端凭据 - WCF: Occasional rejection of client credentials
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM