作法:鎖定企業的端點

大型企業通常需要在遵循企業安全性原則的環境中開發應用程式。 下列主題討論如何開發與安裝用戶端端點驗證器,以用於驗證所有安裝在電腦上的 Windows Communication Foundation (WCF) 用戶端應用程式。

在這種情況下,驗證程式會是用戶端驗證程式,因為這個端點行為會加入 machine.config 檔案中的用戶端 <commonBehaviors> 區段。 WCF 只會為用戶端應用程式載入通用端點行為,並且只會為服務應用程式載入通用服務行為。 若要為服務應用程式安裝這個相同的驗證器,驗證器必須是服務行為。 如需詳細資訊,請參閱 <commonBehaviors> 一節。

重要

當應用程式在部分信任環境中執行時,加入組態檔 <commonBehaviors> 區段但未以 AllowPartiallyTrustedCallersAttribute 屬性 (APTCA) 標記的服務或端點行為不會執行,並且發生這種情況時,也不會擲回例外狀況。 若要強制執行通用行為 (例如驗證器),您必須執行下列其中一項:

  • 使用 AllowPartiallyTrustedCallersAttribute 屬性標記您的通用行為,讓它可以在部署為部分信任應用程式時執行。 請注意,您可以在電腦上設定登錄項目,以防止已標記 APTCA 的組件執行。

  • 確定應用程式是否會部署為完全信任的應用程式,而使用者是否無法修改程式碼存取安全性設定以在部分信任環境中執行應用程式。 如果可以執行這項操作,自訂驗證器就不會執行,也不會擲回例外狀況。 如需確保這項操作的一種方法,請參閱使用程式碼存取安全性原則工具 (Caspol.exe)levelfinal 選項。

如需詳細資訊,請參閱部分信任最佳做法支援的部署案例

若要建立端點驗證器

  1. IEndpointBehavior 方法中所需的驗證步驟建立 Validate。 下列程式碼提供一個範例。 (InternetClientValidatorBehavior 是取自安全性驗證範例。)

    public class InternetClientValidatorBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint serviceEndpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { }
        public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, System.ServiceModel.Dispatcher.ClientRuntime behavior) { }
        public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher) { }
    
        public void Validate(ServiceEndpoint endpoint)
        {
            BindingElementCollection elements = endpoint.Binding.CreateBindingElements();
    
            if (EndpointIsDual(endpoint, elements))
                throw new InvalidOperationException("InternetClientValidator: endpoint uses 'dual' mode. This mode is disallowed for use with untrusted services.");
    
            if (EndpointAllowsNtlm(endpoint, elements))
                throw new InvalidOperationException("InternetClientValidator: endpoint allows NTLM. This mode is disallowed for use with untrusted services.");
    
            if (EndpointAllowsTransactionFlow(endpoint, elements))
                throw new InvalidOperationException("InternetClientValidator: endpoint flows transaction ids. This mode is disallowed for use with untrusted services.");
        }
    
  2. 建立新的 BehaviorExtensionElement,它會註冊步驟 1 中建立的端點驗證器。 下列程式碼範例會顯示這點。 (這個範例的原始程式碼位於安全性驗證範例中。)

    public class InternetClientValidatorElement : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(InternetClientValidatorBehavior); }
        }
    
        protected override object CreateBehavior()
        {
            return new InternetClientValidatorBehavior();
        }
    }
    
  3. 請務必以強式名稱簽署編譯的組件。 如需詳細資訊,請參閱強式名稱工具 (SN.EXE) 和您語言的編譯器命令。

若要將驗證器安裝至目標電腦

  1. 使用適當的機制安裝端點驗證器。 在企業中,您可以使用群組原則和 Systems Management Server (SMS) 安裝端點驗證器。

  2. 使用 Gacutil.exe (全域組件快取工具) 將強式名稱的組件安裝至全域組件快取中。

  3. 使用 System.Configuration 命名空間型別以:

    1. 使用完整類型名稱將延伸模組加入 <behaviorExtensions> 區段,並鎖定元素。

      // Register our validator configuration element.
      ExtensionsSection extensions
        = machine.GetSection(@"system.serviceModel/extensions") as ExtensionsSection;
      if (extensions == null)
        throw new Exception("not extensions section.");
      ExtensionElement validator
        = new ExtensionElement(
          "internetClientValidator",
          "Microsoft.ServiceModel.Samples.InternetClientValidatorElement, InternetClientValidator, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
        );
      validator.LockItem = true;
      if (extensions.BehaviorExtensions.IndexOf(validator) < 0)
        extensions.BehaviorExtensions.Add(validator);
      
    2. 將行為元素加入 <commonBehaviors> 區段的 EndpointBehaviors 屬性,並鎖定元素。 (若要在服務上安裝驗證器,驗證器必須是 IServiceBehavior 並加入至 ServiceBehaviors 屬性。)下列程式碼範例將示範如何在步驟 a. 和 b. 後進行適當的設定,唯一的例外是沒有強式名稱時。

      // Add a new section for our validator and lock it down.
      // Behaviors for client applications must be endpoint behaviors.
      // Behaviors for service applications must be service behaviors.
      CommonBehaviorsSection commonBehaviors
        = machine.GetSection(@"system.serviceModel/commonBehaviors") as CommonBehaviorsSection;
      InternetClientValidatorElement internetValidator = new InternetClientValidatorElement();
      internetValidator.LockItem = true;
      commonBehaviors.EndpointBehaviors.Add(internetValidator);
      
    3. 儲存 machine.config 檔。 下列程式碼範例執行步驟 3 中所有的工作,但將修改過的 machine.config 檔案複本儲存在本機上。

      // Write to disk.
      machine.SaveAs("newMachine.config");
      
      // Write our new information.
      SectionInformation cBInfo = commonBehaviors.SectionInformation;
      Console.WriteLine(cBInfo.GetRawXml());
      Console.WriteLine(extensions.SectionInformation.GetRawXml());
      Console.Read();
      

範例

下列程式碼範例示範如何將通用行為新增至 machine.config 檔案,並將複本儲存至磁碟上。 InternetClientValidatorBehavior 是取自安全性驗證範例。

Configuration machine = ConfigurationManager.OpenMachineConfiguration();
// Register our validator configuration element.
ExtensionsSection extensions
  = machine.GetSection(@"system.serviceModel/extensions") as ExtensionsSection;
if (extensions == null)
  throw new Exception("not extensions section.");
ExtensionElement validator
  = new ExtensionElement(
    "internetClientValidator",
    "Microsoft.ServiceModel.Samples.InternetClientValidatorElement, InternetClientValidator, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
  );
validator.LockItem = true;
if (extensions.BehaviorExtensions.IndexOf(validator) < 0)
  extensions.BehaviorExtensions.Add(validator);

// Add a new section for our validator and lock it down.
// Behaviors for client applications must be endpoint behaviors.
// Behaviors for service applications must be service behaviors.
CommonBehaviorsSection commonBehaviors
  = machine.GetSection(@"system.serviceModel/commonBehaviors") as CommonBehaviorsSection;
InternetClientValidatorElement internetValidator = new InternetClientValidatorElement();
internetValidator.LockItem = true;
commonBehaviors.EndpointBehaviors.Add(internetValidator);
// Write to disk.
machine.SaveAs("newMachine.config");

// Write our new information.
SectionInformation cBInfo = commonBehaviors.SectionInformation;
Console.WriteLine(cBInfo.GetRawXml());
Console.WriteLine(extensions.SectionInformation.GetRawXml());
Console.Read();

.NET Framework 安全性

您可能也要加密組態檔項目。 如需詳細資訊,請參閱「請參閱」一節。

另請參閱