共用方式為


以 TCP 通道進行驗證

TCP 通道直接支援驗證與模擬。這個主題說明如何設定用戶端與伺服器通道。

.NET Framework 可讓遠端物件的伺服器藉由設定關聯之 TcpServerChannelTcpClientChannel 物件的屬性來驗證與模擬呼叫端。

伺服器組態

若要將 TCP 伺服器通道設為驗證遠端呼叫端,請在 TCP 通道上將 secure 屬性設為 true,如下列組態檔所示。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <channels>
              <channel ref="tcp" secure="true" />
             </channels>
        </application>
    </system.runtime.remoting>
</configuration>
Note注意:

若要進行安全通訊,用戶端通道上的 secure 屬性必須同時設為 true。根據預設,安全的通訊包括驗證和加密伺服器與用戶端之間傳送的資料。

經過驗證之用戶端的識別可以藉由取得 CurrentPrincipal 並接著存取其 Identity 屬性來加以存取。經過模擬之用戶端的識別可以由 GetCurrent 來存取。必要時,您可以接著執行自己的驗證作業。

若要將 TCP 伺服器通道設為驗證與模擬遠端呼叫端,請將 secure 屬性與 impersonate 屬性設為 true,如下列組態檔所示。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <channels>
              <channel ref="tcp" secure="true" impersonate="true" />
             </channels>
        </application>
    </system.runtime.remoting>
</configuration>
Note注意:

若要進行模擬,用戶端通道上的 tokenImpersonationLevel 屬性必須設為 impersonationdelegation。請注意,伺服器上的屬性稱為 impersonate,但是在用戶端上,您要將 tokenImpersonationLevel 設為 impersonation

Note注意:

所有處理序都會在 Windows Vista 的標準使用者帳戶底下執行。標準使用者無法模擬管理員帳戶。如果您的遠端處理應用程式必須模擬管理員帳戶,該應用程式必須在更高的權限下執行。

authorizationModule 屬性可讓伺服器執行自己的驗證作業。若要使用此屬性,您必須建立可實作 IAuthorizeRemotingConnection 的類別。這個介面包含下列方法:

  • IsConnectingEndpointAuthorized,可讓您依照 IP 位址授權用戶端。

  • IsConnectingIdentityAuthorized,可讓您依照識別來授權用戶端。

在伺服器組態檔中,您要將 authorizationModule 屬性設為可在格式為 "namespace.class, assembly" 的字串中實作 IAuthorizeRemotingConnection 的類別,如下列組態檔所示。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application name="AuthorizationApp">
      <service>
        <wellknown mode="SingleCall"
                   type="Server.SampleService, Server"
                   objectUri="Server.rem"/>
      </service>

      <channels>
        <channel ref="tcp" port="8001" secure="true" impersonate="true" authorizationModule="Server.AuthorizationModule,Server"/>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

用戶端組態

若要將 TCP 用戶端通道設為驗證呼叫端,請在 TCP 通道上將 secure 屬性設為 true,如下列組態檔所示。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <channels>
              <channel ref="tcp" secure="true" />
             </channels>
        </application>
    </system.runtime.remoting>
</configuration>
Note注意:

若要進行安全通訊,伺服器通道上的 secure 屬性必須同時設為 true。根據預設,安全的通訊包括驗證和加密伺服器與用戶端之間傳送的資料。

若要將 TCP 用戶端通道設為使用模擬,請將 secure 屬性設為 true,並將 tokenImpersonationLevel 屬性設為 impersonation,如下列組態檔所示。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <channels>
              <channel ref="tcp" secure="true" tokenImpersonationLevel="impersonation"/>
             </channels>
        </application>
    </system.runtime.remoting>
</configuration>
Note注意:

若要進行模擬,伺服器通道上的 impersonate 屬性必須設為 true。請注意,伺服器上的屬性稱為 impersonate,但是在用戶端上,tokenImpersonationLevel 會設為 impersonation

Note注意:

所有處理序都會在 Windows Vista 的標準使用者帳戶底下執行。標準使用者無法模擬管理員帳戶。如果您的遠端處理應用程式必須模擬管理員帳戶,該應用程式必須在更高的權限下執行。

tokenImpersonationLevel 可以設為下表中的其中一個值。

tokenImpersonationLevel 設定 說明

識別

伺服器可以取得關於用戶端的資訊,例如安全識別項 (Security Identifier) 和權限,但無法模擬用戶端。

模擬

伺服器可在其本機系統上模擬用戶端的安全性內容。伺服器無法在遠端系統上模擬用戶端。

委派

伺服器可以在遠端系統上模擬用戶端。

根據預設,TCP 用戶端通道可以透過用來執行用戶端處理序的使用者識別來驗證自己。您可以設定 domainusernamepassword 屬性來指定替代識別,如下列程式碼範例所示。

RemotingConfiguration.Configure("Client.exe.config", true);
ISharedInterface remoteObject = (ISharedInterface)Activator.GetObject(
                typeof(ISharedInterface),
                "tcp://localhost:8001/server.rem");

// Set domain, username, and password
IDictionary props = ChannelServices.GetChannelSinkProperties(remoteObject);
props["domain"] = "SomeDomain"; // only required if the user is a member of a domain
props["username"] = "SomeRemotingUser";
props["password"] = "Password123";

// Call method on remote object
remoteObject.HelloWorld("Hi Server");

您也可以指定組態檔中的 domainusernamepassword 屬性。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <channels>
              <channel ref="tcp" 
                       secure="true" 
                       tokenImpersonationLevel="impersonation"
                       username="SomeRemotingUser"
                       password="Password123"
                       />
             </channels>
        </application>
    </system.runtime.remoting>
</configuration>
Note注意:

不建議您在程式碼或組態中使用硬式編碼的使用者名稱與密碼,此處作法僅供說明用途。取得認證的最好方式,就是提示使用者。

根據預設,TCP 通道會使用 NTLM 來驗證呼叫端。若要強制通道使用 Kerberos,請將 servicePrincipalName 屬性設為用來執行服務的帳戶,如下列組態檔所示。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <channels>
              <channel ref="tcp" secure="true" tokenImpersonationLevel="impersonation"
                       servicePrincipalName="MyDomain\MyUserAccount"/>
          </channels>
        </application>
    </system.runtime.remoting>
</configuration>
Note注意:

Kerberos 需要存取 Active Directory 伺服器。如果執行程式碼的電腦不是 Active Directory 網域的成員,請改用 NTLM (預設)。

請參閱

概念

加密和訊息完整性
以 HTTP 通道進行驗證
以 IPC 通道進行驗證

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.