5

Managerここに記載されているタイプのオブジェクトに属性を設定しようとしていUserPrincipalます:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms680857(v=vs.85).aspx

しかし、簡単に言うことはできません

UserPrincipal.Manager = "some value" 

誰かがこれがどのように機能するか説明してもらえますか? ありがとう!

4

2 に答える 2

11

S.DS.AM 名前空間の基本UserPrincipalにはその属性がありませんが、ユーザー プリンシパル クラスを拡張し、必要な属性を追加することができます。

詳細については、こちらをご覧ください。

.NET Framework 3.5 でのディレクトリ セキュリティ プリンシパルの管理
(記事の最後に拡張性に関するセクションがあります)

コードは次のとおりです。

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

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

    // Create the "Manager" property.    
    [DirectoryProperty("manager")]
    public string Manager
    {
        get
        {
            if (ExtensionGet("manager").Length != 1)
                return string.Empty;

            return (string)ExtensionGet("manager")[0];
        }
        set { ExtensionSet("manager", value); }
    }

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

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

これで、使用するプロパティをUserPrincipalEx持つクラスを見つけて操作できます。.Manager

UserPrincipalEx userEx = UserPrincipalEx.FindByIdentity(ctx, "YourUserName");

// the .Manager property contains the DN (distinguished name) for the manager of this user
var yourManager = userEx.Manager;
于 2012-07-02T18:07:52.357 に答える
4

私はこの例が大好きですが、RatBoyStl がどこから来ているのかを完全に理解しています。新しいクラスではなく値だけが必要な場合があります。

特定のユーザーのオブジェクトがある場合はUserPrinciple、このコードで manager プロパティを簡単に取得できます。さらに一歩進んで、マネージャーの値を使用してUserPrinciple、メールアドレスを見つけて表示しました。

            //set the principal context to the users domain
        PrincipalContext pc = new PrincipalContext(ContextType.Domain, userDomain);

        //lookup the user id on the domain
        UserPrincipal up = UserPrincipal.FindByIdentity(pc, userId);
        if (up == null)
        {
            Console.WriteLine(string.Format("AD USER NOT FOUND {0}", userGc));
            return;

        }

        //grab the info we need from the domain
        Console.WriteLine(up.ToString());

        DirectoryEntry d = up.GetUnderlyingObject() as DirectoryEntry;
        string managerCN = d.Properties["manager"].Value.ToString(); 
        Console.WriteLine(managerCN);

        UserPrincipal manager = UserPrincipal.FindByIdentity(pc, managerCN);
        Console.WriteLine(manager.EmailAddress);
于 2013-08-05T21:28:46.767 に答える