Share via


Remotingbeispiel: Hosting in Internetinformationsdiensten (IIS)

Dieses Thema bezieht sich auf eine veraltete Technologie, die zum Zwecke der Abwärtskompatibilität mit vorhandenen Anwendungen beibehalten wird und nicht für die neue Entwicklung empfohlen wird. Verteilte Anwendungen sollten jetzt mit  Windows Communication Foundation (WCF) entwickelt werden.

Im folgenden Beispiel wird ein Basiswebdienst mit ein einigen Komplikationen implementiert. BinaryFormatter wird verwendet, weil das Datenaufkommen stärker ist und das System weniger Zeit zum Serialisieren und Deserialisieren des Streams benötigt. Außerdem authentifiziert der Server den Client und gibt dem Client anschließend die Identität zurück, die IIS authentifizieren konnte, wenn IIS (Internet Information Services, Internetinformationsdienste) die auch als NTLM-Authentifizierung bekannte integrierte Windows-Authentifizierung verwendet. Schließlich können Sie den Webdienst schützen, indem Sie die URL in der Clientkonfigurationsdatei so ändern, dass "https" als Protokollschema verwendet wird, und indem Sie IIS so konfigurieren, dass eine SSL (Secure Sockets Layer)-Verschlüsselung für dieses virtuelle Verzeichnis erforderlich ist. (Im Beispiel wird dieser Prozess nicht veranschaulicht).

c2swb8ah.Caution(de-de,VS.100).gifVorsicht:
.NET Framework-Remoting führt standardmäßig keine Authentifizierung oder Verschlüsselung durch. Daher empfiehlt es sich, vor der Remoteinteraktion mit Clients und Servern alle erforderlichen Schritte zu unternehmen, um die Identität der Clients oder Server sicherzustellen. Da .NET Framework-Remoteanwendungen FullTrust-Berechtigungen zur Ausführung benötigen, könnte ein nicht autorisierter Client Code so ausführen, als ob er voll vertrauenswürdig wäre, wenn dem Client Zugriff auf Ihren Server gewährt würde. Authentifizieren Sie Ihre Endpunkte, und verschlüsseln Sie die Kommunikationsstreams unbedingt, indem Sie Ihre Remotetypen in IIS hosten oder ein benutzerdefiniertes Channelsenkenpaar erstellen, das diese Aufgabe übernimmt.

So kompilieren Sie dieses Beispiel und führen es aus

  1. Speichern Sie alle Dateien in einem Verzeichnis mit dem Namen RemoteIIS.

  2. Kompilieren Sie das ganze Beispiel, indem Sie die folgenden Befehle an der Eingabeaufforderung eingeben:

    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. Erstellen Sie das Unterverzeichnis \bin, und kopieren Sie ServiceClass.dll in dieses Verzeichnis.

  4. Erstellen Sie eine Anwendung in IIS. Geben Sie "HttpBinary" als Anwendungsalias an, und legen Sie für das Quellverzeichnis das Verzeichnis "RemoteIIS" fest.

  5. Legen Sie die Authentifizierungsmethode für dieses virtuelle Verzeichnis auf die integrierte Windows-Authentifizierung (ehemals NTLM-Authentifizierung) fest. Wenn ein anonymer Zugriff ausgewählt ist, nimmt HttpContext.Current.User.Identity.Name den Wert null an und GetServerString gibt "***unavailable***" für den Benutzeralias zurück. Um dies zu verhindern, heben Sie die Auswahl des anonymen Zugriffs auf.

  6. Stellen Sie sicher, dass IIS gestartet wird; geben Sie an der Eingabeaufforderung im Verzeichnis "RemoteIIS" die Zeichenfolge client ein.

Diese Anwendung wird auf einem einzelnen Computer oder über ein Netzwerk ausgeführt. Wenn Sie diese Anwendung über ein Netzwerk ausführen möchten, müssen Sie "localhost" in der Clientkonfiguration durch den Namen des Remotecomputers ersetzen.

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>

Siehe auch

Konzepte

Konfiguration von Remoteanwendungen
Hosten von Remoteobjekten in Internetinformationsdiensten (IIS)

Weitere Ressourcen

Remotingbeispiele