不安全的 Internet 客户端和服务

下图显示了不安全的 Windows Communication Foundation (WCF) 公共客户端和服务的示例:

Screenshot that shows an unsecured Internet scenario

特征 说明
安全模式
Transport HTTP
绑定 代码中的 BasicHttpBinding,或配置中的 <basicHttpBinding> 元素。
互操作性 与现有的 Web 服务客户端和服务进行互操作
Authentication
完整性
机密性

服务

下面的代码和配置应独立运行。 执行下列操作之一:

  • 使用代码(而不使用配置)创建独立服务。

  • 使用提供的配置创建服务,但不定义任何终结点。

代码

下面的代码演示如何创建不安全的终结点。 默认情况下,BasicHttpBinding 将安全模式设置为 None

Uri httpUri = new Uri("http://localhost/Calculator");

// Create the ServiceHost.
ServiceHost myServiceHost = new ServiceHost(typeof(Calculator), httpUri);

// Create a binding that uses HTTP. By default,
// this binding has no security.
BasicHttpBinding b = new BasicHttpBinding();

// Add an endpoint to the service.
myServiceHost.AddServiceEndpoint(typeof(ICalculator), b, "");
// Open the service and wait for calls.
AddMexEndpoint(ref myServiceHost);
myServiceHost.Open();
Console.Write("Listening....");
Console.ReadLine();
// Close the service when a key is pressed.
myServiceHost.Close();
Dim httpUri As New Uri("http://localhost/Calculator")

' Create the ServiceHost.
Dim myServiceHost As New ServiceHost(GetType(Calculator), httpUri)

' Create a binding that uses HTTP. By default, 
' this binding has no security.
Dim b As New BasicHttpBinding()

' Add an endpoint to the service.
myServiceHost.AddServiceEndpoint(GetType(ICalculator), b, "")
' Open the service and wait for calls.
AddMexEndpoint(myServiceHost)
myServiceHost.Open()
Console.Write("Listening....")
Console.ReadLine()
' Close the service when a key is pressed.
myServiceHost.Close()

服务配置

下面的代码使用配置设置相同的终结点。

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <system.serviceModel>  
    <behaviors />  
    <services>  
      <service behaviorConfiguration="" name="ServiceModel.Calculator">  
        <endpoint address="http://localhost/Calculator"
                  binding="basicHttpBinding"  
                  bindingConfiguration="Basic_Unsecured"
                  name="BasicHttp_ICalculator"  
                  contract="ServiceModel.ICalculator" />  
      </service>  
    </services>  
    <bindings>  
      <basicHttpBinding>  
        <binding name="Basic_Unsecured" />  
      </basicHttpBinding>  
    </bindings>  
    <client />  
  </system.serviceModel>  
</configuration>  

客户端

下面的代码和配置应独立运行。 执行下列操作之一:

  • 使用代码(和客户端代码)创建独立客户端。

  • 创建不定义任何终结点地址的客户端。 而使用将配置名称作为自变量的客户端构造函数。 例如:

    CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");
    
    Dim cc As New CalculatorClient("EndpointConfigurationName")
    

代码

下面的代码演示一个访问不安全终结点的基本 WCF 客户端。

// Create an instance of the BasicHttpBinding.
// By default, there is no security.
BasicHttpBinding myBinding = new BasicHttpBinding();

// Create the address string, or get it from configuration.
string httpUri = "http://localhost/Calculator";

// Create an endpoint address with the address.
EndpointAddress myEndpoint = new EndpointAddress(httpUri);

// Create an instance of the WCF client. The client
// code was generated using the Svcutil.exe tool.
CalculatorClient cc = new CalculatorClient(myBinding, myEndpoint);
try
{
    cc.Open();
    // Begin using the calculator.
    Console.WriteLine(cc.Divide(100, 2));

    // Close the client.
    cc.Close();
}
catch (TimeoutException tex)
{
    Console.WriteLine(tex.Message);
    cc.Abort();
}
catch (CommunicationException cex)
{
    Console.WriteLine(cex.Message);
    cc.Abort();
}
finally
{
    Console.WriteLine("Closed the client");
    Console.ReadLine();
}
' Create an instance of the BasicHttpBinding. 
' By default, there is no security.
Dim myBinding As New BasicHttpBinding()

' Create the address string, or get it from configuration.
Dim httpUri As String = "http://localhost/Calculator"

' Create an endpoint address with the address.
Dim myEndpoint As New EndpointAddress(httpUri)

' Create an instance of the WCF client. The client
' code was generated using the Svcutil.exe tool.
Dim cc As New CalculatorClient(myBinding, myEndpoint)
Try
    cc.Open()
    ' Begin using the calculator.
    Console.WriteLine(cc.Divide(100, 2))

    ' Close the client.
    cc.Close()
Catch tex As TimeoutException
    Console.WriteLine(tex.Message)
    cc.Abort()
Catch cex As CommunicationException
    Console.WriteLine(cex.Message)
    cc.Abort()
Finally
    Console.WriteLine("Closed the client")
    Console.ReadLine()
End Try

客户端配置

下面的代码将配置客户端。

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <system.serviceModel>  
    <bindings>  
      <basicHttpBinding>  
        <binding name="BasicHttpBinding_ICalculator" >  
          <security mode="None">  
          </security>  
        </binding>  
      </basicHttpBinding>  
    </bindings>  
    <client>  
      <endpoint address="http://localhost/Calculator/Unsecured"  
          binding="basicHttpBinding"
          bindingConfiguration="BasicHttpBinding_ICalculator"  
          contract="ICalculator"
          name="BasicHttpBinding_ICalculator" />  
    </client>  
  </system.serviceModel>  
</configuration>  

另请参阅