Web services 使用WS-Security动态调用受保护的web服务

Web services 使用WS-Security动态调用受保护的web服务,web-services,wcf,security,ws-security,Web Services,Wcf,Security,Ws Security,我正在开发一个用.NET4.5.1编写的客户端软件。该软件支持调用不同的web服务,其中一些是由WS-Security保护的,而另一些不是 是否有任何方法可以发送嵌入WS-Security的某些请求,而有些请求不可以 非常感谢您调用的每个服务在应用程序的配置文件中都有自己的配置部分 下面是从中复制的示例app.config。请注意,您调用的每个服务都有一个端点元素,并且每个端点元素都可以引用绑定配置。WS-security配置是在绑定元素上完成的,有关WCF安全性的文档,请参阅。基本上,您必须将

我正在开发一个用.NET4.5.1编写的客户端软件。该软件支持调用不同的web服务,其中一些是由WS-Security保护的,而另一些不是

是否有任何方法可以发送嵌入WS-Security的某些请求,而有些请求不可以


非常感谢

您调用的每个服务在应用程序的配置文件中都有自己的配置部分

下面是从中复制的示例
app.config
。请注意,您调用的每个服务都有一个
端点
元素,并且每个
端点
元素都可以引用
绑定
配置。WS-security配置是在绑定元素上完成的,有关WCF安全性的文档,请参阅。基本上,您必须将安全模式设置为
Message
,才能对请求和响应使用WS-security加密

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
        <client>
          <endpoint
            name="endpoint1"
            address="http://localhost/ServiceModelSamples/service.svc"
            binding="wsHttpBinding"
            bindingConfiguration="WSHttpBinding_IHello"
            behaviorConfiguration="IHello_Behavior"
            contract="IHello" >

            <metadata>
              <wsdlImporters>
                <extension
                  type="Microsoft.ServiceModel.Samples.WsdlDocumentationImporter, WsdlDocumentation"/>
              </wsdlImporters>
            </metadata>

            <identity>
              <servicePrincipalName value="host/localhost" />
            </identity>
          </endpoint>
// Add another endpoint by adding another <endpoint> element.
          <endpoint
            name="endpoint2">
           //Configure another endpoint here.
          </endpoint>
        </client>

//The bindings section references by the bindingConfiguration endpoint attribute.
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IHello" 
                 bypassProxyOnLocal="false" 
                 hostNameComparisonMode="StrongWildcard">
          <readerQuotas maxDepth="32"/>
          <reliableSession ordered="true" 
                           enabled="false" />
          <security mode="Message">
           //Security settings go here.
          </security>
        </binding>
        <binding name="Another Binding"
        //Configure this binding here.
        </binding>
          </wsHttpBinding>
        </bindings>

//The behavior section references by the behaviorConfiguration endpoint attribute.
        <behaviors>
            <endpointBehaviors>
                <behavior name=" IHello_Behavior ">
                    <clientVia />
                </behavior>
            </endpointBehaviors>
        </behaviors>

    </system.serviceModel>
</configuration>

//通过添加另一个元素来添加另一个端点。
//在此配置另一个端点。
//bindings部分通过bindingConfiguration端点属性引用。
//安全设置在这里。

您调用的每个服务在应用程序的配置文件中都有自己的配置部分

下面是从中复制的示例
app.config
。请注意,您调用的每个服务都有一个
端点
元素,并且每个
端点
元素都可以引用
绑定
配置。WS-security配置是在绑定元素上完成的,有关WCF安全性的文档,请参阅。基本上,您必须将安全模式设置为
Message
,才能对请求和响应使用WS-security加密

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
        <client>
          <endpoint
            name="endpoint1"
            address="http://localhost/ServiceModelSamples/service.svc"
            binding="wsHttpBinding"
            bindingConfiguration="WSHttpBinding_IHello"
            behaviorConfiguration="IHello_Behavior"
            contract="IHello" >

            <metadata>
              <wsdlImporters>
                <extension
                  type="Microsoft.ServiceModel.Samples.WsdlDocumentationImporter, WsdlDocumentation"/>
              </wsdlImporters>
            </metadata>

            <identity>
              <servicePrincipalName value="host/localhost" />
            </identity>
          </endpoint>
// Add another endpoint by adding another <endpoint> element.
          <endpoint
            name="endpoint2">
           //Configure another endpoint here.
          </endpoint>
        </client>

//The bindings section references by the bindingConfiguration endpoint attribute.
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IHello" 
                 bypassProxyOnLocal="false" 
                 hostNameComparisonMode="StrongWildcard">
          <readerQuotas maxDepth="32"/>
          <reliableSession ordered="true" 
                           enabled="false" />
          <security mode="Message">
           //Security settings go here.
          </security>
        </binding>
        <binding name="Another Binding"
        //Configure this binding here.
        </binding>
          </wsHttpBinding>
        </bindings>

//The behavior section references by the behaviorConfiguration endpoint attribute.
        <behaviors>
            <endpointBehaviors>
                <behavior name=" IHello_Behavior ">
                    <clientVia />
                </behavior>
            </endpointBehaviors>
        </behaviors>

    </system.serviceModel>
</configuration>

//通过添加另一个元素来添加另一个端点。
//在此配置另一个端点。
//bindings部分通过bindingConfiguration端点属性引用。
//安全设置在这里。

谢谢你的回答。其实我不会提前知道什么服务。用户可以通过UI提供服务,我们将发现提供的服务,并调用用户选择的服务的特定方法。有些调用需要WS-Security,但有些调用不需要WS-Security,在这种情况下,您必须在代码中创建端点、绑定和行为对象。我只能在运行时知道连接到哪个服务,如果我提前知道服务,我找到了在代码中创建端点和绑定的方法。但是,如果我不知道该服务,我会找到一种使用反射调用web服务的方法,类似于本文:。但是我不知道如何在这种情况下支持WS-Security谢谢你的回答。其实我不会提前知道什么服务。用户可以通过UI提供服务,我们将发现提供的服务,并调用用户选择的服务的特定方法。有些调用需要WS-Security,但有些调用不需要WS-Security,在这种情况下,您必须在代码中创建端点、绑定和行为对象。我只能在运行时知道连接到哪个服务,如果我提前知道服务,我找到了在代码中创建端点和绑定的方法。但是,如果我不知道该服务,我会找到一种使用反射调用web服务的方法,类似于本文:。但是我不知道在这种情况下如何支持WS-Security