System.DirectoryServices (SDS) 名前空間のクラスを使用して、ユーザとグループを作成するサンプルを掲載します。

確認環境

  • 動作環境:Windows 2003 Active Directory環境
  • 開発環境:Visual Studio 2008 Professional
  • .NET 2.0 以上

1. グループを作成する

グループを作成するサンプルを掲載します。グループの種類には、セキュリティグループ、配布グループという種類とそれぞれに対して、ドメインローカル、グローバルグループ、ユニバーサルグループの区別があります。グループは列挙形 GroupType で定義しています。

/// <summary>
/// ActiveDirectoryグループタイプ
/// </summary>
[Flags]
public enum GroupType : uint
{
    ADS_GROUP_TYPE_BUILTIN_LOCAL_GROUP = 0x00000001,
    ADS_GROUP_TYPE_GLOBAL_GROUP        = 0x00000002,
    ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP  = 0x00000004,
    ADS_GROUP_TYPE_UNIVERSAL_GROUP     = 0x00000008,
    ADS_GROUP_TYPE_SECURITY_ENABLED    = 0x80000000,
    // ドメインローカル配布グループ
    LocalDistribution = ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP,
    // ドメインローカルセキュリティグループ
    LocalSecurity = ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP | ADS_GROUP_TYPE_SECURITY_ENABLED,
    // グローバル配布グループ
    GlobalDistribution = ADS_GROUP_TYPE_GLOBAL_GROUP,
    // グローバルセキュリティグループ
    GlobalSecurity = ADS_GROUP_TYPE_GLOBAL_GROUP | ADS_GROUP_TYPE_SECURITY_ENABLED,
    // ユニバーサル配布グループ
    UniversalDIstribution = ADS_GROUP_TYPE_UNIVERSAL_GROUP,
    // ユニバーサルセキュリティグループ
    UniversalSecurity = ADS_GROUP_TYPE_UNIVERSAL_GROUP | ADS_GROUP_TYPE_SECURITY_ENABLED
}

以降が引数で指定されたグループ名を作成するサンプルです。メソッドのみで定義しています。

/// <summary>
/// 引数で指定された名前のセキュリティグループを作成する
/// </summary>
/// <param name="newGroup"></param>
public static void CreateGroup(string newGroup)
{
    // 作成するグループの親ディレクトリオブジェクト
    using (DirectoryEntry parent = GetEntry("CN=Users,DC=crm1,DC=local"))
    {
        object obj = parent.NativeObject;
        uint groupType = (uint)GroupType.LocalSecurity;
        DirectoryEntry group = parent.Children.Add("CN=" + newGroup, "group");
        group.Properties["sAMAccountName"].Value = newGroup;
        group.Properties["groupType"].Value = (int) groupType;
        group.CommitChanges();
    }
}
public static DirectoryEntry GetEntry(string path)
{
    return new DirectoryEntry("LDAP://" + path, null, null, AuthenticationTypes.Secure);
}

2 グループにメンバーを追加する、削除する方法1

ADSI で定義されているインタフェースIADsGroup の Add , Remove メソッドを使用してメンバーを追加、削除するサンプルを掲載します。追加しようとしているメンバーがグループに含まれているかは IsMember メソッドで確認するようにしています。

/// <summary>
/// ユーザがグループに所属しているか調べ、
/// 所属している場合は削除し、所属していない場合は追加する
/// 削除、追加メソッドにIADsGroup.Add, IADsGroup.Remove メソッドを使用する
/// </summary>
public static void ManageGroup()
{
    using (DirectoryEntry group = GetEntry("CN=testGroup,CN=Users,DC=crm1,DC=local"))
    {
        string crmuserAdsPath = "LDAP://CN=crmuser03,OU=CrmUsers,DC=crm1,DC=local";
        if (!IsMember(group, crmuserAdsPath))
        {
            Console.WriteLine("add member");
            AddMember(group, crmuserAdsPath);
        }
        else
        {
            Console.WriteLine("remove member");
            RemoveMember(group, crmuserAdsPath);
        }
    }
}
public static bool IsMember(DirectoryEntry entry, string adsPath)
{
    return (bool)entry.Invoke("IsMember", adsPath);
}
public static void AddMember(DirectoryEntry entry, string adsPath)
{
    entry.Invoke("Add", adsPath);
}
public static void RemoveMember(DirectoryEntry entry, string adsPath)
{
    entry.Invoke("Remove", adsPath);
}

サンプルでは、crm1.local ドメインのオブジェクトユニット CrmUsers に含まれるユーザ crmuser03 をUsers コンテナの testGroup グループに追加しています。

3 グループにメンバーを追加する、削除する方法2

group のスキーマに含まれる member 属性値にメンバーを追加、削除する方法で、グループにメンバーを追加、削除する方法を次のサンプルで掲載します。

public static void ManageGroup2()
{
    using (DirectoryEntry group = GetEntry("CN=testGroup,CN=Users,DC=crm1,DC=local"))
    {
        try
        {
            string crmuserPath = "CN=crmuser03,OU=CrmUsers,DC=crm1,DC=local";
            // メンバーに追加する場合
              AddMember2(group, crmuserPath);
            // メンバーを削除する場合
              //RemoveMember2(group, crmuserPath);
            group.CommitChanges();
        }
        catch (COMException ex)
        {
            Console.WriteLine(ex.ErrorCode);
        }
    }
}
public static void AddMember2(DirectoryEntry entry, string memberPath)
{
    entry.Properties["member"].Add(memberPath);
}
public static void RemoveMember2(DirectoryEntry entry, string memberPath)
{
    entry.Properties["member"].Remove(memberPath);
}

サンプルでは、IADsGroupのメソッドを使用するサンプルと同様に、crm1.local ドメインのオブジェクトユニット CrmUsers に含まれるユーザ crmuser03 をUsers コンテナの testGroup グループに追加しています。

説明は以上です。誤り等がありましたらご連絡下さい。