简体   繁体   English

如何在C#中的Active Directory中的UserPrincipal对象上设置管理器属性

[英]How do I set the Manager Attribute on the UserPrincipal object in Active Directory in C#

I am trying to set the attribute Manager on an object of type UserPrincipal documented here: 我正在尝试在此处记录的UserPrincipal类型的对象上设置属性Manager

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

but cannot simply say 但不能简单地说

UserPrincipal.Manager = "some value" 

Can someone please explain to me how this works? 有人可以向我解释一下这是如何工作的吗? Thanks! 谢谢!

The basic UserPrincipal in the S.DS.AM namespace does not feature that attribute - but you can extend the user principal class and add additional attributes that you need. S.DS.AM命名空间中的基本UserPrincipal不具有该属性 - 但您可以扩展用户主体类并添加所需的其他属性。

Read more about it here: 在这里阅读更多相关信息:

Managing Directory Security Principals in the .NET Framework 3.5 管理.NET Framework 3.5中的目录安全性主体
( there's a section on extensibility towards the end of the article ) 关于文章末尾的可扩展性部分

Here's the code: 这是代码:

[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);
    }
}

Now you can find and work with a UserPrincipalEx class which has the .Manager property for you to use: 现在,您可以找到并使用具有.Manager属性的UserPrincipalEx类供您使用:

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

// the .Manager property contains the DN (distinguished name) for the manager of this user
var yourManager = userEx.Manager;

I love the example but totally get where the RatBoyStl is coming from. 我喜欢这个例子,但完全得到了RatBoyStl的来源。 Sometimes you just want a value and not a new class. 有时你只想要一个值而不是一个新类。

If you have a UserPrinciple object for a given user then you can easily retrieve the manager property with this code. 如果您有给定用户的UserPrinciple对象,则可以使用此代码轻松检索manager属性。 I took it a step futher and used the manager value to find their UserPrinciple and display the email address. UserPrinciple ,使用管理器值找到他们的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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为了通过C#UserPrincipal在Active Directory中设置UserCannotChangePassword,我必须具有哪些权限才能委派 - What permissions do I have to delegate in order to set UserCannotChangePassword in Active Directory through a C# UserPrincipal 如何使用 C# 中的 UserPrincipal 获取 Active Directory 中的地址详细信息 - How to get details of address in Active Directory using UserPrincipal in C# C# Active Directory PrincipalContext / UserPrincipal.IsMemberOf 错误 - C# Active Directory PrincipalContext / UserPrincipal.IsMemberOf error 给定一个UserPrincipal对象,如何从Active Directory中获取“公司”和“部门”? - How to get “Company” and “Department” from Active Directory given a UserPrincipal object? 如何清除Active Directory中的用户对象属性? - How do I clear out a user object attribute in Active Directory? 如何使用ASP.net,C#通过PrincipalContext在活动目录上添加Manager属性 - How to add Manager attribute on active directory by PrincipalContext using ASP.net,C# C#UserPrincipal对象引用未设置为对象的实例 - C# UserPrincipal Object reference not set to an instance of an object UserPrincipal对象,Active Directory查询:DirectoryServicesCOMException - UserPrincipal Object, Active Directory Query: DirectoryServicesCOMException 如何从员工的UserPrincipal对象获取管理器的UserPrincipal对象 - How to get a UserPrincipal object for the manager from the employee's UserPrincipal object 如何使用目录服务(C#)在Active Directory中设置gecos属性 - How to set gecos attribute in Active Directory using directory services(C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM