• 记录:查看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>

     

    运行结果:

     

     

  • 相关阅读:
    用asp.net(C#)写的论坛
    javascript:window.history.go(1)什么意思啊?
    5个有趣的 JavaScript 代码片段
    marquee属性的使用说明
    flash网站欣赏
    获取验证码程序
    ACCESS中执行sql语句
    靠谱的工程师
    mysql变量(用户+系统)
    理解进程和线程
  • 原文地址:https://www.cnblogs.com/LeimOO/p/1559258.html
Copyright © 2020-2023  润新知