Partilhar via


Intranet Unsecured Client and Service

The following illustration depicts a simple Windows Communication Foundation (WCF) service developed to provide information on a secure private network to a WCF application. Security is not required because the data is of low importance, the network is expected to be inherently secure, or security is provided by a layer below the WCF infrastructure.

Intranet unsecured client and service scenario

Characteristic Description

Security Mode

None

Transport

TCP

Binding

NetTcpBinding

Interoperability

WCF only

Authentication

None

Integrity

None

Confidentiality

None

Service

The following code and configuration are meant to run independently. Do one of the following:

  • Create a stand-alone service using the code with no configuration.

  • Create a service using the supplied configuration, but do not define any endpoints.

Code

The following code shows how to create an endpoint with no security:

Dim tcpUri As New Uri("net.tcp://localhost:8008/Calculator")

' Create the ServiceHost.
Dim sh As New ServiceHost(GetType(Calculator), tcpUri)

' Create a binding that uses TCP and set the security mode to none.
Dim b As New NetTcpBinding()
b.Security.Mode = SecurityMode.None

' Add an endpoint to the service.
sh.AddServiceEndpoint(GetType(ICalculator), b, "")
' Open the service and wait for calls.
sh.Open()

Dim listenUri As String = sh.Description.Endpoints(0).ListenUri.AbsoluteUri
Console.WriteLine("Listening on: {0}", listenUri)
Console.Write("Press Enter to end the service")
Console.ReadLine()
' Close the service when a key is pressed.
Uri tcpUri = new Uri("net.tcp://localhost:8008/Calculator");

// Create the ServiceHost.
ServiceHost sh = new ServiceHost(typeof(Calculator), tcpUri);

// Create a binding that uses TCP and set the security mode to none.
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.None;

// Add an endpoint to the service.
sh.AddServiceEndpoint(typeof(ICalculator), b, "");
// Open the service and wait for calls.
sh.Open();

string listenUri = sh.Description.Endpoints[0].ListenUri.AbsoluteUri;
Console.WriteLine("Listening on: {0}", listenUri);
Console.Write("Press Enter to end the service");
Console.ReadLine();
// Close the service when a key is pressed.

Configuration

The following code sets up the same endpoint using configuration:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors />
    <services>
      <service behaviorConfiguration="" 
               name="ServiceModel.Calculator">
        <endpoint address="net.tcp://localhost:8008/Calculator" 
                  binding="netTcpBinding"
                  bindingConfiguration="tcp_Unsecured" 
                  name="netTcp_ICalculator"
                  contract="ServiceModel.ICalculator" />
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="tcp_Unsecured">
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
    <client />
  </system.serviceModel>
</configuration>

Client

The following code and configuration are meant to run independently. Do one of the following:

  • Create a stand-alone client using the code (and client code).

  • Create a client that does not define any endpoint addresses. Instead, use the client constructor that takes the configuration name as an argument. For example:

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

Code

The following code shows a basic WCF client that accesses an unsecured endpoint using the TCP protocol.

' Create an instance of the NetTcpBinding and set the
' security mode to none.
Dim myBinding As New NetTcpBinding()
myBinding.Security.Mode = SecurityMode.None

' Create the address string, or get it from configuration.
Dim tcpUri As String = "net.tcp://machineName:8008/Calculator"

' Create an endpoint address with the address.
Dim myEndpointAddress As New EndpointAddress(tcpUri)

' Create an instance of the WCF client. The client
' code was generated using the Svcutil.exe tool.
Dim cc As New CalculatorClient(myBinding, myEndpointAddress)
Try
    cc.Open()
// Create an instance of the NetTcpBinding and set the
// security mode to none.
NetTcpBinding myBinding = new NetTcpBinding();
myBinding.Security.Mode = SecurityMode.None;

// Create the address string, or get it from configuration.
string tcpUri = "net.tcp://machineName:8008/Calculator";

// Create an endpoint address with the address.
EndpointAddress myEndpointAddress = new EndpointAddress(tcpUri);

// Create an instance of the WCF client. The client
// code was generated using the Svcutil.exe tool.
CalculatorClient cc = new CalculatorClient(myBinding, myEndpointAddress);
try
{
    cc.Open();

Configuration

The following configuration code applies to the client:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_ICalculator" >
          <security mode="None">
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint address="net.tcp://machineName:8008/Calculator "
                binding="netTcpBinding" 
                bindingConfiguration="NetTcpBinding_ICalculator"
                contract="ICalculator" 
                name="NetTcpBinding_ICalculator" />
    </client>
  </system.serviceModel>
</configuration>

See Also

Reference

NetTcpBinding

Concepts

Security Overview