加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用LDAP从Active Directory获取所有用户的详细信息

发布时间:2020-12-15 20:47:46 所属栏目:百科 来源:网络整理
导读:我需要使用LDAP从Active Directory获取所有用户的详细信息.以下代码确实将Samaccountname作为“管理员”,但不是每个用户的详细信息,并且在列表中找不到邮件ID.请帮助. string dominName = ConfigurationManager.AppSettings["DominName"].ToString();string
我需要使用LDAP从Active Directory获取所有用户的详细信息.以下代码确实将Samaccountname作为“管理员”,但不是每个用户的详细信息,并且在列表中找不到邮件ID.请帮助.

string dominName = ConfigurationManager.AppSettings["DominName"].ToString();
string ldapPath = ConfigurationManager.AppSettings["ldapPath"].ToString();
if (!String.IsNullOrEmpty(dominName) && !String.IsNullOrEmpty(ldapPath))
{
    DirectoryEntry entry = new DirectoryEntry(ldapPath,txtUsername.Text.ToString().Trim(),txtPassword.Text.ToString().Trim());
    try
    {
        Object obj = entry.NativeObject;
        DirectorySearcher search = new DirectorySearcher(entry);
        search.Filter = "(&(objectClass=user)(objectCategory=person))";
        search.PropertiesToLoad.Add("samaccountname");
        search.PropertiesToLoad.Add("mail");
        search.PropertiesToLoad.Add("usergroup");
        search.PropertiesToLoad.Add("displayname");//first name

        foreach (System.DirectoryServices.SearchResult resEnt in search.FindAll())
        {    
            System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry();
            if (de.Properties["sAMAccountName"].Value != null && de.Properties["userAccountControl"].Value!=null)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Name = " + de.Properties["sAMAccountName"].Value.ToString());
                sb.AppendLine("Email = " + de.Properties["Mail"].Value.ToString());
            }
        }

找到解决方案

这是我的代码:

var userAccountControlValue = 0;
int.TryParse(de.Properties["UserAccountControl"].Value.ToString(),out userAccountControlValue);
var isAccountDisabled = Convert.ToBoolean(userAccountControlValue & 0x0002);
var isNormalAccount = Convert.ToBoolean(userAccountControlValue & 0x0200);
if (de.Properties["sAMAccountName"].Value != null && de.Properties["userAccountControl"].Value != null && de.Properties["userPrincipalName"].Value != null && !isAccountDisabled && isNormalAccount)
{
    //Add Employee details from AD
    PaySlipPortal.Objects.Employee employee = new Employee();
    employee.FirstName = de.Properties["givenName"].Value!=null?(string)de.Properties["givenName"].Value:"";
    employee.Email = de.Properties["userPrincipalName"].Value != null ? (string)de.Properties["userPrincipalName"].Value : "";
    employee.LastName = de.Properties["sn"].Value != null ? 

    (string)de.Properties["sn"].Value : "";
    int deleteID=  empBL.DeleteEmployee(employee.Email.Trim());
    int empID = empBL.AddEmployee(employee);  

}

解决方法

尝试查看“mail”属性(不是“Mail”).

sb.AppendLine("Email = " + de.Properties["mail"].Value.ToString());

这是AD用户属性参考(如果您想获得其他内容):http://www.kouti.com/tables/userattributes.htm

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读