当前位置:编程学习 > C#/ASP.NET >>

使用DirectoryEntry怎么取AD某一组的用户

 public static void IsUserValid3(string psPath, string UserName, string Password, string psField)
        {
            string strName = UserName + psField;
            using (DirectoryEntry deUser = new DirectoryEntry(@"LDAP://" + psPath, strName, Password))//(@"LDAP://192.168.83.100", strName, Password))
            {
                 Console.WriteLine(deUser.Children.SchemaFilter.Count.ToString());

                try
                {
                    Object native = deUser.NativeObject;

                    DirectorySearcher src = new DirectorySearcher(deUser);
                   // src.Filter = "(SAMAccountName= " + UserName + ") ";
                    src.Filter = ("(objectClass=user)"); //user表示用户,group表示组

                    src.PropertiesToLoad.Add("cn ");

                    src.SearchRoot = deUser;
                    src.SearchScope = SearchScope.Subtree;
                    //SearchResult result = src.FindOne();

                    //DirectoryEntry objDirEnt = new DirectoryEntry(result.Path, strName, Password, AuthenticationTypes.Secure);

                    //DirectorySearcher s = new DirectorySearcher(objDirEnt);

                    //DirectoryEntry user = resEnt.GetDirectoryEntry();

                    // SearchResult res = s.FindAll();

                    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 src.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"))
                        {
                            Console.WriteLine(user.Properties["sAMAccountName"][0].ToString());
                            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;

                    Console.WriteLine(dt.Rows.Count.ToString());
                }
                catch (Exception ex)
                {
                    //string message = ex.Message;
                    Console.WriteLine(ex.Message);
                }
                Console.ReadKey();
            }
        }





使用DirectoryEntry怎么取AD某一组的用户
主要想知道怎么去过滤组 --------------------编程问答-------------------- --------------------编程问答-------------------- 你的值取到了吗
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,