如何在Asp.net C#中使用LDAP搜索Active Directory中的用户 [英] How to Search User in Active Directory using LDAP in Asp.net C#

查看:143
本文介绍了如何在Asp.net C#中使用LDAP搜索Active Directory中的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从asp.net c#中的Active Directory服务器中获取所有记录?

How can I fetch all the record from my Active Directory Server in asp.net c#?

推荐答案

LDAP 中建立连接字符串,提供用户名密码,与服务器通信并具有管理员权限.

Make a connection string in LDAP providing username and Password which can communicate with the server and have Administrator rights.

假定DC为 me.com ,并且用户名密码是具有管理员权限<.

Suppose DC is me.com and username and password are the password of that user Id which is having Administrator rights.

   DirectoryEntry rootDSE = rootDSE = new DirectoryEntry("LDAP://OU="",OU=" ",dc="me",dc=com", username, password);

    DirectorySearcher search = new DirectorySearcher(rootDSE);

    search.PageSize = 1001;// To Pull up more than 100 records.

     search.Filter = "(&(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))";//UserAccountControl will only Include Non-Disabled Users.
      SearchResultCollection result = search.FindAll();

         foreach (SearchResult item in result)
        {
            if (item.Properties["cn"].Count > 0)
            {
                DisplayName = item.Properties["cn"][0].ToString();
            }
            if (item.Properties["mail"].Count > 0)
            {
                EmailAddress = item.Properties["mail"][0].ToString();
            }
            if (item.Properties["SamAccountName"].Count > 0)
            {
                DomainName = item.Properties["SamAccountName"][0].ToString();
            }
            if (item.Properties["department"].Count > 0)
            {
                Department = item.Properties["department"][0].ToString();
            }
            if (item.Properties["title"].Count > 0)
            {
                title = item.Properties["title"][0].ToString();
            }
            if (item.Properties["company"].Count > 0)
            {
                company = item.Properties["company"][0].ToString();
            }
            if (item.Properties["DistinguishedName"].Count > 0)
            {
                memberof = item.Properties["DistinguishedName"][0].ToString();
            }
            if (item.Properties["AccountExpirationDate"].Count > 0)
            {
                string aaa = item.Properties["AccountExpirationDate"][0].ToString();
            }

              dt.Rows.Add(DisplayName, EmailAddress, DomainName, Department, title, company, memberof);
             DisplayName = string.Empty;
             EmailAddress = string.Empty;
             DomainName = string.Empty;
             Department = string.Empty;
             title = string.Empty;
             company = string.Empty;
             memberof = string.Empty;

               rootDSE.Dispose();

通过这种方式,我们可以从域服务器中提取所有记录.

In this way we can Pull up all the records from our Domain Server.

这篇关于如何在Asp.net C#中使用LDAP搜索Active Directory中的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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