如何使用端点的 TransportWithMessageCredential 安全模式验证 wsdl get?

Posted

技术标签:

【中文标题】如何使用端点的 TransportWithMessageCredential 安全模式验证 wsdl get?【英文标题】:How to authenticate wsdl get with TransportWithMessageCredential security mode for the endpoint? 【发布时间】:2020-11-20 05:32:39 【问题描述】:

我有一个 WCF 端点,它使用 basicHttpBinding 公开 API。此出价设置为使用安全模式TransportWithMessageCredentialand UserNameclientCredentialType

由于安全性是在消息级别实现的,因此在 WCF 中,IIS 需要允许匿名访问。因此,无需提供任何凭据即可获取 wsdl。

如何强制认证获取服务元数据?

这里当前的服务配置看起来像(来自 web.config)

<system.serviceModel> 
  <bindings>
    <basicHttpBinding>
      <binding name="secure">
        <security mode="TransportWithMessageCredential">
          <message clientCredentialType="UserName" />
        </security>   
      </binding>     
      </basicHttpBinding>
  </bindings>
  <services>
    <service behaviorConfiguration="secure" name="someProject.MyService">
      <endpoint  binding="basicHttpBinding" contract="someProject.IService" bindingConfiguration="secure"  />   
    </service>
  </services>
  <behaviors>    
    <serviceBehaviors>
        <behavior name="secure">          
        <serviceMetadata httpsGetEnabled="true"  />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

我尝试了显而易见的方法,通过使用服务行为配置为元数据设置特定绑定:

<behavior name="secure">          
   <serviceMetadata httpsGetEnabled="true" httpsGetBinding="basicHttpBinding" httpsGetBindingConfiguration="transportSecure" />
</behavior>

//and add the new binding    
  <basicHttpBinding>
      <binding name="transportSecure">
        <security mode="Transport">
          <message clientCredentialType="UserName" />
        </security>
      </binding>
    </basicHttpBinding>

但不支持。它抛出这个:

MessageVersion 'Soap11 (http://schemas.xmlsoap.org/soap/envelope/) 寻址无 (http://schemas.microsoft.com/ws/2005/05/addressing/none)' 不是 在这种情况下支持。只有 MessageVersion 'EnvelopeNone (http://schemas.microsoft.com/ws/2005/05/envelope/none) 地址无 (http://schemas.microsoft.com/ws/2005/05/addressing/none)' 是 支持。

我不明白这个错误或如何解决它。

【问题讨论】:

【参考方案1】:

通常我们不会在生产环境中公开我们的元数据,但是如果你想启用元数据,我们可以使用https绑定来保护元数据。

1.使用适当的 X.509 证书配置端口。证书必须来自受信任的权威机构,并且必须具有“服务授权”的预期用途。您必须使用 HttpCfg.exe 工具将证书附加到端口。

2.创建ServiceMetadataBehavior类的新实例。

3.将ServiceMetadataBehavior类的HttpsGetEnabled属性设置为true。

4.将 HttpsGetUrl 属性设置为适当的 URL。请注意,如果您指定绝对地址,则 URL 必须以方案 https:// 开头。如果指定相对地址,则必须为服务主机提供 HTTPS 基地址。如果未设置此属性,则默认地址为“”,或直接位于服务的 HTTPS 基地址。

5.将实例添加到ServiceDescription类的Behaviors属性返回的行为集合中,如下代码所示。

ServiceMetadataBehavior sb = new ServiceMetadataBehavior();
sb.HttpsGetEnabled = true;
sb.HttpsGetUrl = new Uri("https://myMachineName:8036/myEndpoint");
myServiceHost.Description.Behaviors.Add(sb);

myServiceHost.Open();

这是在 WCF 上启用的身份验证,你也可以在 IIS 上启用 windows 身份验证,这两种方法都可以保护元数据。

但是在生产环境中,我不建议你开启元数据,因为这样会导致元数据泄露的风险。WCF服务的调用也可以通过通道工厂来调用。在这种情况下,我们可以在不知道服务器元数据的情况下调用 WCF 服务。

有关如何保护元数据的更多信息,您可以参考此链接:

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-secure-metadata-endpoints?redirectedfrom=MSDN

【讨论】:

以上是关于如何使用端点的 TransportWithMessageCredential 安全模式验证 wsdl get?的主要内容,如果未能解决你的问题,请参考以下文章

如何测试使用图像的 FastAPI api 端点?

如何安全地使用 MVC WebAPI OData 端点?

如何使用请求正文调用 API 端点?

如何保护默认的Zuul端点?

如何正确使用身份服务器 4 的自省端点?

如何使用端点的 TransportWithMessageCredential 安全模式验证 wsdl get?