Delen via


Niet-beveiligde intranetclient en -service

In de volgende afbeelding ziet u een eenvoudige WCF-service (Windows Communication Foundation) die is ontwikkeld om informatie te verstrekken over een beveiligd particulier netwerk voor een WCF-toepassing. Beveiliging is niet vereist omdat de gegevens van laag belang zijn, het netwerk naar verwachting inherent veilig is of beveiliging wordt geboden door een laag onder de WCF-infrastructuur.

Intranet unsecured client and service scenario.

Characteristic Beschrijving
Beveiligingsmodus Geen
Transport TCP
Binding NetTcpBinding
Interoperabiliteit Alleen WCF
Verificatie Geen
Integriteit Geen
Vertrouwelijkheid Geen

Service

De volgende code en configuratie zijn bedoeld om onafhankelijk van elkaar te worden uitgevoerd. Voer een van de volgende stappen uit:

  • Maak een zelfstandige service met behulp van de code zonder configuratie.

  • Maak een service met behulp van de opgegeven configuratie, maar definieer geen eindpunten.

Code

De volgende code laat zien hoe u een eindpunt maakt zonder beveiliging:

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.

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.

Configuratie

Met de volgende code wordt hetzelfde eindpunt ingesteld met behulp van de configuratie:

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

Klant

De volgende code en configuratie zijn bedoeld om onafhankelijk van elkaar te worden uitgevoerd. Voer een van de volgende stappen uit:

  • Maak een zelfstandige client met behulp van de code (en clientcode).

  • Maak een client die geen eindpuntadressen definieert. Gebruik in plaats daarvan de clientconstructor die de configuratienaam als argument gebruikt. Voorbeeld:

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

Code

De volgende code toont een eenvoudige WCF-client die toegang heeft tot een onbeveiligd eindpunt met behulp van het TCP-protocol.

// 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();
' 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()

Configuratie

De volgende configuratiecode is van toepassing op de 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>  

Zie ook