Share via


Exemple de communication à distance : hébergement dans les Services Internet (IIS)

Cette rubrique est spécifique à la technologie héritée assurant la compatibilité descendante avec des applications existantes et n'est pas recommandée en cas de nouveau développement. Les applications distribuées doivent maintenant être développées à l'aide de Windows Communication Foundation (WCF)

L'exemple suivant implémente un service Web de base avec quelques subtilités. Le BinaryFormatter est utilisé, car la charge utile est plus compacte et le système prend moins de temps pour sérialiser et désérialiser le flux. De plus, si les Services Internet (IIS) utilisent l'authentification intégrée à Windows (également connue sous le nom d'authentification NTLM), le serveur authentifie le client, puis lui retourne l'identité qu'IIS a pu authentifier. Enfin, vous pouvez protéger votre service Web en modifiant l'URL dans le fichier configuration client de façon à utiliser le schéma de protocole « https » et en configurant IIS pour qu'il requière le chiffrement Secure Sockets Layer (SSL) pour ce répertoire virtuel (l'exemple ne montre pas ce processus).

c2swb8ah.Caution(fr-fr,VS.100).gifAttention :
Par défaut, .NET Framework Remoting ne procède ni à l'authentification ni au chiffrement. Par conséquent, il est recommandé que vous preniez toutes les mesures nécessaires à l'identification des clients et des serveurs avant d'interagir à distance avec eux. Comme les applications .NET Framework Remoting requièrent des autorisations FullTrust pour s'exécuter, un client non autorisé pourrait exécuter du code comme s'il était d'un niveau de confiance suffisant s'il se voyait accorder l'accès à votre serveur. Veillez à toujours authentifier vos points de terminaison et à chiffrer les flux de données de communication, soit en hébergeant vos types distants dans IIS, soit en générant une paire de récepteurs du canal personnalisée dédiée à cette usage.

Pour compiler et exécuter cet exemple

  1. Enregistrez tous les fichiers dans un répertoire nommé RemoteIIS.

  2. Compilez l'intégralité de l'exemple en saisissant les commandes suivantes dans l'invite de commandes :

    vbc /t:library ServiceClass.vb
    vbc /r:System.Runtime.Remoting.dll /r:ServiceClass.dll Client.vb
    
    csc /t:library ServiceClass.cs
    csc /r:System.Runtime.Remoting.dll /r:ServiceClass.dll Client.cs
    
  3. Créez un sous-répertoire \bin et copiez-y ServiceClass.dll.

  4. Créez une application dans IIS. Créez l'alias d'application « HttpBinary » et définissez le répertoire « RemoteIIS » comme répertoire source.

  5. Définissez la méthode d'authentification pour ce répertoire virtuel à authentification Windows intégrée (anciennement authentification NTLM). Si l'accès anonyme est sélectionné, HttpContext.Current.User.Identity.Name sera Null et GetServerString retournera "***unavailable***" pour l'alias d'utilisateur. Pour éviter ceci, désélectionnez l'accès anonyme.

  6. Assurez-vous qu'IIS est démarré et saisissez « client » dans l'invite de commandes, à partir du répertoire « RemoteIIS ».

Cette application peut s'exécuter sur un seul ordinateur ou sur un réseau. Pour exécuter cette application sur un réseau, remplacez "localhost" dans la configuration client par le nom de l'ordinateur distant.

ServiceClass

Imports System
Imports System.Runtime.Remoting
Imports System.Web

Public Interface IService
    Function GetServerTime() As DateTime
    Function GetServerString() As String
End Interface

Public Class ServiceClass
    Inherits MarshalByRefObject
    Implements IService

    Private InstanceHash As Integer

    Public Sub New()
        InstanceHash = Me.GetHashCode()
    End Sub

    Public Function GetServerTime() As Date Implements IService.GetServerTime
        Return DateTime.Now
    End Function

    Public Function GetServerString() As String Implements IService.GetServerString
        ' Use the HttpContext to acquire what IIS thinks the client's identity is.
        Dim temp As String = HttpContext.Current.User.Identity.Name

        If (temp Is Nothing Or temp.Equals(String.Empty)) Then
            temp = "**unavailable**"
        End If

        Return "Hi there. You are being served by instance number: " _
            & InstanceHash.ToString() _
            & ". Your alias is: " _
            & temp
    End Function
End Class
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Threading;
using System.Web;

public interface IService
{
   DateTime GetServerTime();
   string GetServerString();
}

// IService exists to demonstrate the possibility of publishing only the interface.
public class ServiceClass : MarshalByRefObject, IService
{
   private int InstanceHash;

   public ServiceClass()
   {
      InstanceHash = this.GetHashCode();
   }

   public DateTime GetServerTime()
   {
      return DateTime.Now;
   }

   public string GetServerString()
   {
      // Use the HttpContext to acquire what IIS thinks the client's identity is.
      string temp = HttpContext.Current.User.Identity.Name;
      if (temp == null || temp.Equals(string.Empty))
         temp = "**unavailable**";
      return "Hi there. You are being served by instance number: " 
         + InstanceHash.ToString() 
         + ". Your alias is: " 
         + temp;
   }
}

Web.config

<configuration>
   <system.runtime.remoting>
      <application>
         <service>
            <wellknown 
               mode="SingleCall" objectUri="SAService.rem"
               type="ServiceClass, ServiceClass"/>
         </service>
         <channels>
            <channel ref="http"/>
         </channels>
      </application>
   </system.runtime.remoting>
</configuration>

Client

Imports System
Imports System.Collections
Imports System.Net
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Security.Principal

Public Class Client

    Public Shared Sub Main()
        ' Tells the system about the remote object and customizes the HttpChannel
        ' to use the binary formatter (which understands that base64 encoding is needed).
        RemotingConfiguration.Configure("Client.exe.config", False)

        ' New proxy for the ServiceClass.
        ' If you publish only the IService interface, you must use Activator.GetObject.
        Dim service As ServiceClass = New ServiceClass()

        ' Programmatically customizes the properties given to the channel. This sample uses the
        '  application configuration file.
        Dim Props As IDictionary = ChannelServices.GetChannelSinkProperties(service)
        Props.Item("credentials") = CredentialCache.DefaultCredentials

        ' Reports the client identity name.
        Console.WriteLine("ConsoleIdentity: " & WindowsIdentity.GetCurrent().Name)

        ' Writes what the server returned.
        Console.WriteLine("The server says : " & service.GetServerString())
        Console.WriteLine("Server time is: " & service.GetServerTime())
    End Sub
End Class
using System;
using System.Collections;
using System.Net;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Security.Principal;

class Client
{
    static void Main(string[] args)
    {
        // Tells the system about the remote object and customizes the HttpChannel
        // to use the binary formatter (which understands that base64 encoding is needed).
        RemotingConfiguration.Configure("Client.exe.config", false);

        // New proxy for the ServiceClass.
        // If you publish only the IService interface, you must use Activator.GetObject.
        ServiceClass service = new ServiceClass();

        // Programmatically customizes the properties given to the channel. This sample uses the
        // application configuration file.
        IDictionary Props = ChannelServices.GetChannelSinkProperties(service);
        Props["credentials"] = CredentialCache.DefaultCredentials;

        // Reports the client identity name.
        Console.WriteLine("ConsoleIdentity: " + WindowsIdentity.GetCurrent().Name);

        // Writes what the server returned.
        Console.WriteLine("The server says : " + service.GetServerString());
        Console.WriteLine("Server time is: " + service.GetServerTime());
    }
}

Client.exe.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <channels>
        <channel ref="http" useDefaultCredentials="true" port="0">
          <clientProviders>
            <formatter 
               ref="binary"
                  />
          </clientProviders>
        </channel>
      </channels>
      <client>
        <wellknown 
           url="https://localhost:80/HttpBinary/SAService.rem"
           type="ServiceClass, ServiceClass"
            />
      </client>
    </application>
  </system.runtime.remoting>
</configuration>

Voir aussi

Concepts

Configuration d'applications distantes
Hébergement d'objets distants dans les Services Internet (IIS)

Autres ressources

Exemples de communication à distance