zoukankan      html  css  js  c++  java
  • 记录:查看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>

     

    运行结果:

     

     

  • 相关阅读:
    Java线程之Callable和Future
    Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
    hdu 6201 transaction transaction transaction
    三分钟读懂TT猫分布式、微服务和集群之路
    springcloud(十):服务网关zuul初级篇
    springcloud(九):配置中心和消息总线(配置中心终结版)
    springcloud(八):配置中心服务化和高可用
    springcloud(七):配置中心svn示例和refresh
    springcloud(六):配置中心git示例
    最简单的SpringBoot整合MyBatis教程
  • 原文地址:https://www.cnblogs.com/LeimOO/p/1559258.html
Copyright © 2011-2022 走看看