zoukankan      html  css  js  c++  java
  • 获取Ad用户信息

    private const string domainName = "ms.com";
            
    private const string adAdmin = "administrator";
            
    private const string password = "pass@word1";
            
    private const string ouName = "XX有限公司";

            
    private DataTable GetADUsers()
            {
               DataTable dt 
    = new DataTable();
               dt.Columns.Add(
    "sAMAccountName");//帐号
                dt.Columns.Add("Name");//姓名
                dt.Columns.Add("mail"); //邮箱地址
                dt.Columns.Add("OU");  //用户组织

                DirectoryEntry adRoot 
    = new DirectoryEntry("LDAP://" + domainName, adAdmin, password, AuthenticationTypes.Secure);
               DirectoryEntry ou 
    = adRoot.Children.Find("OU=" + ouName);

               DirectorySearcher mySearcher 
    = new DirectorySearcher(ou);
               mySearcher.Filter 
    = ("(objectClass=user)"); //user表示用户,group表示组

                
    foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
                {
                    DataRow dr 
    = dt.NewRow();
                    dr[
    "sAMAccountName"= string.Empty;
                    dr[
    "Name"= string.Empty;
                    dr[
    "mail"= string.Empty;
                    dr[
    "OU"= string.Empty;

                    DirectoryEntry user 
    = resEnt.GetDirectoryEntry();
                    
    if (user.Properties.Contains("sAMAccountName"))
                    {
                        dr[
    "sAMAccountName"= user.Properties["sAMAccountName"][0].ToString();
                    }
                    
    if (user.Properties.Contains("Name"))
                    {
                        dr[
    "Name"= user.Properties["Name"][0].ToString();
                    }
                    
    if (user.Properties.Contains("mail"))
                    {
                        dr[
    "mail"= user.Properties["mail"][0].ToString();
                    }
                    
    if (user.Parent.Name != string.Empty && user.Parent.Name.IndexOf('='> -1)
                    {
                        
    //获取用户所在的组织单位
                           dr["OU"= user.Parent.Name.Split('=')[1];
                    }
                    dt.Rows.Add(dr);
                }
                
    return dt;
            }
        }

    如果想要知道用户信息中都包含哪些字段,可以foreach出来看看

     DirectoryEntry user = resEnt.GetDirectoryEntry();  

         foreach (string property in user.Properties.PropertyNames)    

       {     

          Console.WriteLine("字段名: " + property);   

       }

     private DataTable GetADUsersAllProperty()
            {
                Boolean
    done = true;
                DataTable dt = new DataTable();
              
     DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domainName);        
      

               DirectorySearcher mySearcher = new
    DirectorySearcher(adRoot);
               mySearcher.Filter =
    ("(objectClass=user)"); //user表示用户,group表示组

                foreach
    (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
          
         {
                    DirectoryEntry user =
    resEnt.GetDirectoryEntry();
                    if (done)
                  
     {
                        foreach (string property in
    user.Properties.PropertyNames)
                        {
                        
       dt.Columns.Add(property);
                        }
                      
     done = false;
                    }

                    DataRow dr =
    dt.NewRow();
                    for (int i = 0; i < dt.Columns.Count;
    i++)
                    {
                        if
    (user.Properties.Contains(dt.Columns[i].ColumnName))
                      
     {
                            dr[i] =
    user.Properties[dt.Columns[i].ColumnName][0].ToString();
                      
     }else dr[i] =
    string.Empty;

                    }  
                  
     dt.Rows.Add(dr);
                }
                return dt;
            }

  • 相关阅读:
    Qt计算器开发(三):执行效果及项目总结
    [HNOI2019]校园旅行
    How to fix nuget Unrecognized license type MIT when pack
    How to fix nuget Unrecognized license type MIT when pack
    git 通过 SublimeMerge 处理冲突
    git 通过 SublimeMerge 处理冲突
    git 上传当前分支
    git 上传当前分支
    gif 格式
    gif 格式
  • 原文地址:https://www.cnblogs.com/914556495wxkj/p/3419071.html
Copyright © 2011-2022 走看看