我怎样才能更好的查询在Active Directory中的多个域使用C#? [英] How can I better query multiple domains in Active Directory using C#?

查看:159
本文介绍了我怎样才能更好的查询在Active Directory中的多个域使用C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扩大仅从搜索在当前登录的域来搜索所有的域在公元一LDAP / AD搜索。该方法采用字符串与查询和回报,并返回一个LDAPInformation对象。

虽然我是问,有没有更好的方法来搜索这个名字不是这样?这是用户不友好,由于需要使用通配符,如果想找一个人按姓氏(例如:母鹿*)。

 公共静态LDAPInformation [] GetGlobalAddressListVIAName(字符串nameQuery)
    {
        变种currentForest = Forest.GetCurrentForest();
        变种globalCatalog = currentForest.FindGlobalCatalog();

        使用(VAR搜索= globalCatalog.GetDirectorySearcher())
        {
            使用(VAR项=新的DirectoryEntry(searcher.SearchRoot.Path))
            {
                sea​​rcher.Filter =(及(或mailNickname = *)(objectClass的=用户)(的displayName =+ nameQuery +));
                sea​​rcher.PropertyNamesOnly = TRUE;
                sea​​rcher.SearchScope = SearchScope.Subtree;
                sea​​rcher.Sort.Direction = SortDirection.Ascending;
                sea​​rcher.Sort.PropertyName =显示名;
                返回searcher.FindAll()铸造<信息搜索结果>()选择(结果=>新建LDAPInformation(result.GetDirectoryEntry()))。的ToArray();
            }
        }
    }
 

下面是对象:

 类LDAPInformation
{
    内部LDAPInformation(的DirectoryEntry项)
    {
        //部分:HASH
        this.sAMAccountName =(字符串)entry.Properties [sAMAccountName赋]值。

        //节:电子邮箱
        this.Mail =(字符串)entry.Properties [电子邮件]值。

        //科:Organziation
        this.Description =(字符串)entry.Properties [说明]值。
        this.Company =(字符串)entry.Properties [公司]值。
        this.Title =(字符串)entry.Properties [标题]值。
        。this.Department =(字符串)entry.Properties [部门]值;

        //科:姓名
        this.DisplayName =(字符串)entry.Properties [显示名]值。
        this.FirstName =(字符串)entry.Properties [名字]值。
        this.MiddleName =(字符串)entry.Properties [middleName]值。
        this.LastName =(字符串)entry.Properties [姓]值。

        //科:地址
        this.StreetAddress =(字符串)entry.Properties [的StreetAddress]值。
        this.City =(字符串)entry.Properties [城市]值。
        。this.State =(字符串)entry.Properties [状态]值;
        this.Postal code =(字符串)entry.Properties [邮政code]值。
        this.TelephoneNumber =(字符串)entry.Properties [telephoneNumber]值。
    }

    公共字符串显示名称
    {
        得到;
        私定;
    }

    公共字符串的邮件
    {
        得到;
        私定;
    }

    公共字符串sAMAccountName赋
    {
        得到;
        私定;
    }

    公共字符串描述
    {
        得到;
        私定;
    }

    公共字符串公司
    {
        得到;
        私定;
    }

    公共字符串标题
    {
        得到;
        私定;
    }

    公共字符串部
    {
        得到;
        私定;
    }

    公共字符串名字
    {
        得到;
        私定;
    }

    公共字符串MiddleName
    {
        得到;
        私定;
    }

    公共字符串名字
    {
        得到;
        私定;
    }

    公共字符串的StreetAddress
    {
        得到;
        私定;
    }

    公共字符串市
    {
        得到;
        私定;
    }

    公共字符串国家
    {
        得到;
        私定;
    }

    公共字符串邮政code
    {
        得到;
        私定;
    }

    公共字符串TelephoneNumber
    {
        得到;
        私定;
    }
}
 

解决方案

查询全局编录是正确的做法。

您可能要考虑Ambigous名称解析(ANR) - http://support.microsoft.com / KB / 243299

I am attempting to expand a LDAP / AD search from only searching in the currently logged in domain to searching all domains in the AD. The method takes in the string with the query and returns and return an LDAPInformation object.

While I am asking, is there any better way to search for the name than in this way? It is user unfriendly due to needing to use wildcards if looking for a person by last name (example: Doe*).

    public static LDAPInformation[] GetGlobalAddressListVIAName(string nameQuery)
    {
        var currentForest = Forest.GetCurrentForest();
        var globalCatalog = currentForest.FindGlobalCatalog();

        using (var searcher = globalCatalog.GetDirectorySearcher())
        {
            using (var entry = new DirectoryEntry(searcher.SearchRoot.Path))
            {
                searcher.Filter = "(&(mailnickname=*)(objectClass=user)(displayName=" + nameQuery + "))";
                searcher.PropertyNamesOnly = true;
                searcher.SearchScope = SearchScope.Subtree;
                searcher.Sort.Direction = SortDirection.Ascending;
                searcher.Sort.PropertyName = "displayName";
                return searcher.FindAll().Cast<SearchResult>().Select(result => new LDAPInformation(result.GetDirectoryEntry())).ToArray();
            }
        }
    }

Here is the object:

    class LDAPInformation
{
    internal LDAPInformation(DirectoryEntry entry)
    {
        //Section: HASH
        this.sAMAccountName = (string)entry.Properties["sAMAccountName"].Value;

        //Section: Email
        this.Mail = (string)entry.Properties["mail"].Value;

        //Section: Organziation
        this.Description = (string)entry.Properties["description"].Value;
        this.Company = (string)entry.Properties["company"].Value;
        this.Title = (string)entry.Properties["title"].Value;
        this.Department = (string)entry.Properties["department"].Value;

        //Section: Name
        this.DisplayName = (string)entry.Properties["displayName"].Value;
        this.FirstName = (string)entry.Properties["firstName"].Value;
        this.MiddleName = (string)entry.Properties["middleName"].Value;
        this.LastName = (string)entry.Properties["lastName"].Value;

        //Section: Address
        this.StreetAddress = (string)entry.Properties["streetAddress"].Value;
        this.City = (string)entry.Properties["city"].Value;
        this.State = (string)entry.Properties["state"].Value;
        this.PostalCode = (string)entry.Properties["postalCode"].Value;
        this.TelephoneNumber = (string)entry.Properties["telephoneNumber"].Value;
    }

    public string DisplayName
    {
        get;
        private set;
    }

    public string Mail
    {
        get;
        private set;
    }

    public string sAMAccountName
    {
        get;
        private set;
    }

    public string Description
    {
        get;
        private set;
    }

    public string Company
    {
        get;
        private set;
    }

    public string Title
    {
        get;
        private set;
    }

    public string Department
    {
        get;
        private set;
    }

    public string FirstName
    {
        get;
        private set;
    }

    public string MiddleName
    {
        get;
        private set;
    }

    public string LastName
    {
        get;
        private set;
    }

    public string StreetAddress
    {
        get;
        private set;
    }

    public string City
    {
        get;
        private set;
    }

    public string State
    {
        get;
        private set;
    }

    public string PostalCode
    {
        get;
        private set;
    }

    public string TelephoneNumber
    {
        get;
        private set;
    }
}

解决方案

Querying the global catalog is the correct approach.

You might want to look into Ambigous Name Resolution (ANR) - http://support.microsoft.com/kb/243299.

这篇关于我怎样才能更好的查询在Active Directory中的多个域使用C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆