代码之家  ›  专栏  ›  技术社区  ›  John Wu

您能用WCF配置更改BehaviorExtension吗?

  •  5
  • John Wu  · 技术社区  · 6 年前

    我的站点调用一个需要一组非常复杂的身份验证协议的服务(我们称之为FooService)。所有协议都封装在自定义ClientCredentials行为中,该行为在代码中声明如下:

    class FooServiceCredentialsBehavior : ClientCredentials 
    {
        public FooServiceCredentialsBehavior()
        {
            //Set up service certificate
            var cert = CertStore.FindBySerialNumber(certSerialNumber);
            base.ServiceCertificate.DefaultCertificate = cert;
        }
    }
    

    然后注册行为扩展:

      <behaviorExtensions>
        <add name="FooServiceCredentials" type="MyProject.Namespace.FooService, MyProject" />
      </behaviorExtensions>
    

    配置endpointBehavior以使用它:

    <endpointBehaviors>
        <behavior name="FooServiceCredentialsBehavior">
          <FooServiceCredentials />
        </behavior>
    

    并设置端点以使用它:

    <endpoint address="https://fooservice.com/bar"
              behaviorConfiguration="FooServiceCredentialsBehavior"
              contract="FooService_PortType" />
    

    以上各项工作完美,多年来为众多客户服务。

    我无法修改FooServiceCredentials类

    base.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
    

    但我不能。

    我想知道是否有可能添加WCF配置,这是适用于自定义凭据行为,将做同样的事情。像这样:

    <endpointBehaviors>
        <behavior name="FooServiceCredentialsBehavior">
          <FooService>
              <ServiceCertificate>
                  <authentication certificateValidationMode="None"/>
              </ServiceCertificate>
          </FooService>
        </behavior>
    

    有可能吗?怎么用?

    1 回复  |  直到 6 年前
        1
  •  1
  •   AleÅ¡ Doganoc    6 年前

    官方文件说这是可能的。检查以下链接: authentication of serviceCertificate Element

    <behavior name="FooServiceCredentialsBehavior">
      <FooServiceCredentials>
          <serviceCertificate>
            <authentication certificateValidationMode="None" revocationMode="NoCheck" />
          </serviceCertificate>
      </FooServiceCredentials>
    </behavior>
    

    如果这不起作用,还有一个可能的选项,不使用behaviorExtension并在clientCredentials配置中直接指定类,如下所示:

    <behavior name="FooServiceCredentialsBehavior">
      <clientCredentials type="FooNamespace.FooServiceCredentialsBehavior, FooAssemblyName">
          <serviceCertificate>
            <authentication certificateValidationMode="None" revocationMode="NoCheck" />
          </serviceCertificate>
      </clientCredentials>
    </behavior>