记录:查看Active Directory里的用户的属性

 

Code

 

// LDAP路径:这里的路径格式要求"LDAP://lhvm.com/ OU=价格中心,DC=lhvm,DC=com "

private static readonly string strADPath = ConfigurationManager.AppSettings["ADPath"].ToString();

// 角色模拟的用户名,最好是管理员名

private static readonly string strADUser = ConfigurationManager.AppSettings["ADAdminUser"].ToString();

// 角色模拟的用户密码

private static readonly string strADPassword = ConfigurationManager.AppSettings["ADPassword"].ToString();

// 当前域的域名

private static readonly string strADDomain = ConfigurationManager.AppSettings["ADDomain"].ToString();

 

public static void TestSearch(string keyword)

{

    string filter = string.Format("(&(|(objectCategory=person))(displayname=*{0}*))", keyword);

 

    try

    {

        DirectorySearcher searcher = new DirectorySearcher();

        searcher.SearchRoot = new DirectoryEntry(strADPath, strADUser, strADPassword);

        searcher.SearchScope = SearchScope.Subtree;

        searcher.Filter = filter;

        SearchResultCollection results = searcher.FindAll();

 

        if (results.Count > 0)

        {

            SearchResult result = results[0];

            foreach (object o in result.Properties.PropertyNames)

            {

                Console.WriteLine( o.ToString()+" : "+result.Properties[o.ToString()][0]);

            }

            byte[] bs,bs1 = new byte[] { };

            bs = (byte[])result.Properties["objectsid"][0];

            bs1 = (byte[])result.Properties["objectguid"][0];

        }

    }

    catch (Exception ex)

    {

        LogHelper.WriteException(ex);

    }

}

 

App.config文件:

<appSettings>

        <add key="ADPath" value="LDAP://DC=DevForMossVM,DC=com"/>

        <add key="ADAdminUser" value="DevForMossVM\administrator"/>

        <add key="ADPassword" value="abc-123"/>

        <add key="ADDomain" value="DevForMossVM.com"/>

</appSettings>

 

运行结果:

 

 

原文地址:https://www.cnblogs.com/LeimOO/p/1559258.html