次の方法で共有


Setting a Binary Property Using System.DirectoryServices

Setting binary metabase properties is more complicated than setting String, DWORD, or list properties. Binary values are represented as an array of objects, each representing a byte using a two-character hex string.

Example Code

The following example shows you how to use the C# programming language to add a certificate hash value to the SSLCertHash metabase property.

This example requires Windows XP Professional Service Pack 2 or Windows Server 2003 Service Pack 1.

Note

System.DirectoryServices can be used to get and set String and DWORD properties in the IIS metabase, and invoke most methods. However, you cannot set binary properties unless you are using Windows XP Professional with Service Pack 2 or Windows Server 2003 with Service Pack 1.

To keep this code example concise, it does not include code access security (CAS) parameters or parameter checking. For more information, see Code Access Security and Validating User Input to Avoid Attacks. Additionally, you can instantiate your System.DirectoryServices.DirectoryEntry object with an authentication parameter.

using System;
using System.IO;
using System.DirectoryServices;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;

namespace System_DirectoryServices_DirectoryEntry_ConfigIIS
{
  class Program
  {
    static void Main(string[] args)
    {


...


SetBinaryProperty("IIS://Localhost/W3SVC", "SSLCertHash", "a1,b2,c3,d4,e5,f6,07,18,29,3a,4b,5c");
ClearProperty("IIS://Localhost/W3SVC", "SSLCertHash");


...


}


...


static void SetBinaryProperty(string metabasePath, string propertyName, string newValue)
{
  //  metabasePath is of the form "IIS://<servername>/<path>"
  //    for example "IIS://localhost/W3SVC" 
  //  propertyName is of the form "<binaryPropertyName>", for example "SSLCertHash"
  //  value is of the form "<hashCode>", for example, "a1,b2,c3,d4,e5,f6,07,18,29,3a,4b,5c"
  Console.WriteLine("\nSetting binary property at {0}/{1} to {2}:", metabasePath, propertyName, newValue);

  try
  {
      DirectoryEntry path = new DirectoryEntry(metabasePath);
      PropertyValueCollection propValues = path.Properties[propertyName];
      object oldObj = propValues.Value;
      Console.WriteLine(" Old value of {0} is {1}", propertyName, DisplayObject(oldObj));

      newValue = newValue.Trim();
      string[] newValueSplit = newValue.Split(new Char[] { ',' });
      object[] newObj = new object[newValueSplit.Length];
      newValueSplit.CopyTo(newObj, 0);

      propValues.Clear();
      propValues.Add(newObj);
      path.CommitChanges();

      path = new DirectoryEntry(metabasePath);
      propValues = path.Properties[propertyName];
      oldObj = propValues.Value;
      Console.WriteLine(" New value of {0} is {1}", propertyName, DisplayObject(oldObj));
      Console.WriteLine(" Done.");
  }
  catch (Exception ex)
  {
      if ("HRESULT 0x80005006" == ex.Message)
          Console.WriteLine(" Property {0} does not exist at {1}", propertyName, metabasePath);
      else
          Console.WriteLine("Failed in SetBinaryProperty with the following exception: \n{0}", ex.Message);
  }
}

static string DisplayObject(object x)
{
    string toReturn = "";
    if (null != x)
    {
        object[] a = x as object[];
        for (int i = 0; i < a.Length; ++i)
        {
            toReturn = toReturn + a[i] + " ";
        }
        return (toReturn);
    }
    else
        return ("not set");
}

static void ClearProperty(string metabasePath, string propertyName)
{
  //  metabasePath is of the form "IIS://<servername>/<path>"
  //    for example "IIS://localhost/W3SVC/1" 
  //  propertyName is of the form "<propertyName>", for example "SSLCertHash"
  Console.WriteLine("\nClearing property at {0}/{1}:", metabasePath, propertyName);

  try
  {
      DirectoryEntry path = new DirectoryEntry(metabasePath);
      PropertyValueCollection propValues = path.Properties[propertyName];
      propValues.Clear();
      path.CommitChanges();
      Console.WriteLine(" Done.");
  }
  catch (Exception ex)
  {
      Console.WriteLine("Failed in ClearProperty with the following exception: \n{0}", ex.Message);
  }
}


...


  }
}
Imports System
Imports System.IO
Imports System.DirectoryServices
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Collections

Module Program

    Sub Main(ByVal args() As String)


...


End Sub


...


End Module