プリンシパルの拡張

アカウント管理 API には、UserPrincipalGroupPrincipal、および ComputerPrincipal という、3 つの具体的なディレクトリ オブジェクト クラスが用意されています。ディレクトリ スキーマは変更可能ですが、これらのプリンシパル オブジェクトのプロパティによってスキーマのすべての属性が表されるわけではないので、開発者は、独自のカスタム プリンシパル オブジェクトを作成することが必要になる場合があります。Principal クラス、AuthenticablePrincipal クラス、UserPrincipal クラス、ComputerPrincipal クラス、および GroupPrincipal クラスはすべて拡張して、オブジェクト モデルを拡張するカスタム オブジェクトを作成することができます。

拡張されたクラスは、オブジェクト モデルに新しいプロパティを追加したり、拡張されたクラスから既存のプロパティを削除したりすることができます。ディレクトリのスキーマでは、拡張されたクラスに追加されたプロパティをサポートする必要があります。新しいプロパティの種類をサポートするために拡張できるのは、アプリケーション ディレクトリとドメイン スキーマのみです。Machine SAM ディレクトリは拡張をサポートしません。拡張オブジェクトを作成するには、アカウント管理 API でここに示されている属性が必要になります。

  • DirectoryPropertyAttribute プリンシパル オブジェクト プロパティとディレクトリ内の対応するオブジェクトのマップです。この属性は、派生クラスのプロパティで設定されます。DirectoryPropertyAttribute クラスには、SchemaAttributeName プロパティが含まれています。
  • DirectoryRdnPrefixAttribute オブジェクトを作成するために使用する RDN プレフィックスです。このプロパティは、派生クラスで設定されます。派生クラスでは、この属性はオプションです。派生クラスで RDN プレフィックスが設定されていない場合、アカウント管理 API により既定の RDN プレフィックス “CN” が提供されます。DirectoryRdnPrefixAttribute クラスには、RdnPrefix プロパティが含まれています。
  • DirectoryObjectClassAttribute ストアがディレクトリ内の新しいオブジェクトに挿入する、スキーマのオブジェクト クラスです。DirectoryObjectClassAttribute クラスには、ObjectClass プロパティが含まれています。

次のコード例は、ユーザー プリンシパル クラスを拡張する方法を示しています。RDN プレフィックスは既定の値 CN に設定され、InetOrgPerson オブジェクト クラスはディレクトリに追加されます。このクラスを通して、ディレクトリ プロパティ mobile も追加されます。

ExtensionGet メソッドおよび ExtensionSet メソッドを使用して、拡張クラスのプロパティの作成と設定を行います。これらのメソッドは、ここに示されている拡張クラスの例で、広く使用されています。

[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("inetOrgPerson")]
public class InetOrgPerson : UserPrincipal
{
    // Inplement the constructor using the base class constructor. 
    public InetOrgPerson(PrincipalContext context) : base(context)
    {

    }
     
    // Implement the constructor with initialization parameters.    
    public InetOrgPerson(PrincipalContext context, 
                         string samAccountName, 
                         string password, 
                         bool enabled)
                         : base(context, 
                                samAccountName, 
                                password, 
                                enabled)
    {
    }

    InetOrgPersonSearchFilter searchFilter;

    new public InetOrgPersonSearchFilter AdvancedSearchFilter
    {
        get
        {
            if ( null == searchFilter )
                searchFilter = new InetOrgPersonSearchFilter(this);

            return searchFilter;
        }
    }

    // Create the mobile phone property.    
    [DirectoryProperty("mobile")]
    public string MobilePhone
    {
        get
        {
            if (ExtensionGet("mobile").Length != 1)
                return null;

            return (string)ExtensionGet("mobile")[0];
        }

        set
        {
            ExtensionSet( "mobile", value );
        }
    }
    
    // Create the other home phone property.    
    [DirectoryProperty("otherHomePhone")]
    public string[] HomePhoneOther
    {
        get
        {
            int len = ExtensionGet("otherHomePhone").Length;

            string[] otherHomePhone = new string[len];
            object[] otherHomePhoneRaw = ExtensionGet("otherHomePhone");

            for( int i = 0; i < len; i++ )
            {
                otherHomePhone[i] = (string)otherHomePhoneRaw[i];
            }
            
            return otherHomePhone;
        }

        set
        {
            ExtensionSet( "otherHomePhone", value );
        }
    }    
    
    // Create the logoncount property.    
    [DirectoryProperty("LogonCount")]
    public Nullable<int> LogonCount
    {
        get
        {
            if (ExtensionGet("LogonCount").Length != 1)
                return null;

            return ((Nullable<int>)ExtensionGet("LogonCount")[0]);
        }
    }

    // Implement the overloaded search method FindByIdentity.
    public static new InetOrgPerson FindByIdentity(PrincipalContext context, 
                                                   string identityValue)
    {
        return (InetOrgPerson)FindByIdentityWithType(context,
                                                     typeof(InetOrgPerson),
                                                     identityValue);
    }

    // Implement the overloaded search method FindByIdentity. 
    public static new InetOrgPerson FindByIdentity(PrincipalContext context, 
                                                   IdentityType identityType, 
                                                   string identityValue)
    {
        return (InetOrgPerson)FindByIdentityWithType(context, 
                                                     typeof(InetOrgPerson), 
                                                     identityType,
                                                     identityValue);
    } 


        
}

    public class InetOrgPersonSearchFilter : AdvancedFilters
    {
        public InetOrgPersonSearchFilter(Principal p) : base(p) { }
        public void LogonCount(int value, MatchType mt)
        {
            this.AdvancedFilterSet("LogonCount", value, typeof(int), mt);
        }

    }

拡張されたクラスの使用

次のコード例は、新しい InetOrgPerson オブジェクトを作成し、ストアに挿入する方法を示しています。また、InetOrgPerson オブジェクトを検索する機能の使用方法も示しています。

            // Create the context for the InetOrgPerson.
            using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "fabrikam", "DC=fabrikam,DC=com"))
            {

                // Create the InetOrgPerson object and set some properties on it.
                InetOrgPerson newInetPerson = new InetOrgPerson(ctx);
                newInetPerson.DisplayName = "Jim Daly";
                newInetPerson.SamAccountName = "Jim Daly";
                newInetPerson.MobilePhone = "2155550189";
                string[] phone = new string[1];
                phone[0] = "1234567";
                newInetPerson.HomePhoneOther = phone;
                newInetPerson.Save();

                // Search the directory for the new object. 
                InetOrgPerson inetPerson = InetOrgPerson.FindByIdentity(ctx,
                                                         IdentityType.SamAccountName,
                                                         "Jim Daly");



                // Search for all Inetorgperson objects that have logged on more than 5 times.
                InetOrgPerson filter = new InetOrgPerson(ctx);
                filter.AdvancedSearchFilter.LogonCount(5, MatchType.GreaterThan);
                PrincipalSearcher ps = new PrincipalSearcher(filter);

                foreach (InetOrgPerson p in ps.FindAll())
                {

                }
            }

グループ オブジェクト

GetMembers メソッドによりグループ オブジェクトが拡張されると、拡張されたグループ内に拡張されたオブジェクトの種類が発生する可能性があります。グループ オブジェクトは、拡張されたオブジェクトの種類を認識していないので、アカウント管理 API は AuthenticablePrincipal オブジェクトを作成して、拡張された種類であるグループのメンバを表します。AuthenticablePrincipal オブジェクトのインターフェイスを使用すると、グループに含まれている拡張されたオブジェクトの種類を管理することができます。アプリケーションは、StructuralObjectClass プロパティを使用して、オブジェクトの種類を決定し、より多くのオブジェクト固有の編集を実行することもできます。

関連項目

リファレンス

System.DirectoryServices.AccountManagement

概念

System.DirectoryServices.AccountManagement について
System.DirectoryServices.AccountManagement の使用

Send comments about this topic to Microsoft.

Copyright © 2008 by Microsoft Corporation. All rights reserved.