如何以编程方式分配WCF ClientCredentials ServiceCertificate属性?

时间:2011-08-24 19:31:03

标签: c# wcf https

我有一个WCF服务通过https托管,并带有自签名证书。我在以编程方式创建绑定时遇到了麻烦:特别是端点行为的一部分。

我的服务配置如下所示:

  <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>

          <endpointBehaviors>
            <behavior name="DisableServiceCertificateValidation">
              <clientCredentials>
                <serviceCertificate>
                  <authentication certificateValidationMode="None"
                                  revocationMode="NoCheck" />
                </serviceCertificate>
              </clientCredentials>
            </behavior>
          </endpointBehaviors>

        </behaviors>
        <bindings>
            <customBinding>
                <binding name="ContactEmail.Web.EmailService.customBinding0">
                    <binaryMessageEncoding />
                  <httpsTransport/>
                </binding>
            </customBinding>
        </bindings>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
        <services>
            <service name="ContactEmail.Web.EmailService">
                <endpoint address="https://xxxxxxxxxxxxxx/EmailService/EmailService.svc" binding="customBinding" bindingConfiguration="ContactEmail.Web.EmailService.customBinding0" contract="ContactEmail.Web.EmailService" behaviorConfiguration="DisableServiceCertificateValidation" />
                <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
            </service>
        </services>
    </system.serviceModel>

当我使用“添加服务引用”功能时,生成的客户端按预期工作。鉴于我打电话设置这样的证书验证回调:

System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);

但是,我不是通过客户端上的配置文件提供服务配置,而是需要以编程方式设置它,因为该调用将是共享库的一部分。所以,我试图通过向客户端构造函数提供我自己的参数来实现这一点,如:

var myClient = new EmailServiceClient(GetBinding(), new EndpointAddress(Strings.EmailServiceEndpointAddress));

在GetBinding()中,我使用BindingElements创建CustomBinding,如HttpsTransportBindingElement,BinaryMessageEncodingBindingElement和SecurityBindingElement.CreateSecureConversationBindingElement(SecurityBindingElement.CreateUserNameOverTransportBindingElement())。

你知道我如何指定诸如certificateValidationMode =“None”和revocationMode =“NoCheck”之类的东西,或者我是否做错了什么?

2 个答案:

答案 0 :(得分:1)

您的ClientBase(EmailServiceClient)上应该有一个Credentials属性(类型为ClientCredentials)(http://msdn.microsoft.com/en-us/library/ms733836.aspx)...并且应该有一个ServiceCertificate属性: http://msdn.microsoft.com/en-us/library/system.servicemodel.description.clientcredentials.servicecertificate.aspx

答案 1 :(得分:1)

SecureConversation是WS-SecureConversation =&gt;的实现高级消息级安全性,其中在第一次调用服务期间创建特殊安全性令牌(通过作为参数传递给创建绑定元素的消息安全性模式进行身份验证),此令牌用于保护后续消息。此安全性还可以形成安全上下文或安全会话。

您在配置文件中的当前绑定未使用SecureConversation,因此代码中定义的绑定与您的服务不兼容。

相关问题