【问题标题】:Can you change a BehaviorExtension with WCF configuration?您可以使用 WCF 配置更改 BehaviorExtension 吗?
【发布时间】:2019-03-11 11:38:06
【问题描述】:

我的站点调用一个需要一组非常复杂的身份验证协议的服务(我们称之为 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" />

以上所有功能都完美无缺,多年来一直为许多客户服务。

我现在将这些东西部署到无法访问 CRL 服务器的系统中,并且自定义行为包括启用了验证的服务证书。所以我需要关闭验证。但是我无法修改 FooServiceCredentials 类。如果可以,我会这样做:

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

但我做不到。

我想知道是否可以添加应用于自定义凭据行为的 WCF 配置来执行相同的操作。像这样的:

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

这个确切的 XML 不起作用(服务甚至不会启动),但我希望有一些神奇的方法可以安排这些标签以仅从配置中禁用服务证书验证。

有可能吗?怎么样?

【问题讨论】:

标签: c# .net wcf servicebehavior


【解决方案1】:

官方文档说可以做到。检查以下链接:authentication of serviceCertificate Element

我认为使用 behaviorExtension 配置,您应该可以使用以下配置,因为您从 ClientCredentials 继承:

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

【讨论】:

  • 第一个选项正是我所需要的,并且有效!这是一个奇迹!谢谢@AlesD。
猜你喜欢
  • 1970-01-01
  • 2011-01-25
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 2013-01-15
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多