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

     

    运行结果:

     

     

  • 相关阅读:
    iOS优化内存方法推荐
    Swift和OC,是编译型语言、解释性语言、运行时语言
    redis常用命令
    redis 基本类型
    spring中事务配置
    redis 基本概览
    ThreadLocal 类说明
    spring 中 AOP 功能
    ps抠图简单方法
    nginx配置文件中location说明
  • 原文地址:https://www.cnblogs.com/LeimOO/p/1559258.html
Copyright © 2020-2023  润新知