C# - 在活动目录中查找用户管理器

时间:2022-11-20
本文介绍了C# - 在活动目录中查找用户管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

开始使用 System.DirectoryServices.AccountManagement 命名空间,对活动目录 (AD) 中的用户执行查找.我还需要用户的管理器,但我似乎在使用这个命名空间时遇到了障碍.获取一个人的当前代码:

Started using the System.DirectoryServices.AccountManagement namespace, to perform the lookup on a user in active directory (AD). I also need the user's manager, but I seem to have hit a bump in the road using this namespace. Current code to get a person:

class Person {
    // Fields
    public string GivenName = null;
    public string Surname = null;
    public string DistinguishedName = null;
    public string Email = null;
    public string MangerDistinguishedName = null;  // Unable to set this

    // Constructor
    public Person(string userName) {
        UserPrincipal user = null;

        try {
            user = GetUser(userName);

            if (user != null) {
                this.GivenName = user.GivenName;
                this.Surname = user.Surname;
                this.DistinguishedName = user.DistinguishedName;
                this.Email = user.EmailAddress;
                this.MangerDistinguishedName = user.<NO SUCH PROPERTY TO FIND A MANAGER'S DISTINGUISHED NAME>
            }
            else {
                throw new MissingPersonException("Person not found");
            }
        }
        catch (MissingPersonException ex) {
            MessageBox.Show(
                ex.Message
                , ex.reason
                , MessageBoxButtons.OK
                , MessageBoxIcon.Error
            );
        }
        catch (Exception ex) {
            MessageBox.Show(
                ex.Message
                , "Error: Possible connection failure, or permissions failure to search for the username provided."
                , MessageBoxButtons.OK
                , MessageBoxIcon.Error
            );
        }
        finally {
            user.Dispose();
        }
    }

执行人物搜索

    private UserPrincipal GetUser(string userName) {
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);

        return user;
    }

还有什么方法可以直接访问特定用户的经理的专有名称?

  • 可能的部分答案此处VB,但我不认为提及经理.
  • 另一个可能的部分问题这里,同样,与经理无关.
  • Possible partial answer here in VB, but I see nothing about referring to managers.
  • Another possible partial one here, again, nothing about managers.

推荐答案

如果您使用 .NET 3.5 及更高版本并使用 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间,您可以轻松扩展现有的 UserPrincipal 类以获得更高级的属性,例如 Manager 等.

If you're on .NET 3.5 and up and using the System.DirectoryServices.AccountManagement (S.DS.AM) namespace, you can easily extend the existing UserPrincipal class to get at more advanced properties, like Manager etc.

在此处阅读所有相关信息:

Read all about it here:

  • 在 .NET Framework 3.5 中管理目录安全主体莉>
  • 有关 System.DirectoryServices.AccountManagement 的 MSDN 文档

基本上,您只需定义一个基于 UserPrincipal 的派生类,然后定义您想要的其他属性:

Basically, you just define a derived class based on UserPrincipal, and then you define your additional properties you want:

[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 "Department" property.    
    [DirectoryProperty("department")]
    public string Department
    {
        get
        {
            if (ExtensionGet("department").Length != 1)
                return string.Empty;

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

    // 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 的扩展"版本:

Now, you can use the "extended" version of the UserPrincipalEx in your code:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // Search the directory for the new object. 
    UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser");

    // you can easily access the Manager or Department now
    string department = inetPerson.Department;
    string manager = inetPerson.Manager;
}        

这篇关于C# - 在活动目录中查找用户管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:C# 使用不同的用户凭据访问活动目录 下一篇:查找属于两个 Active Directory 组的用户

相关文章

最新文章