如何只获取一组Active Directory中的用户列表 [英] How Can I Get Only List Of Users In A Group Of Active Directory

查看:78
本文介绍了如何只获取一组Active Directory中的用户列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须得到一组Active Directory中的用户列表当用户选择组名时,我有不同的组,它应该填写网格视图中该组的用户列表



i我使用下面的代码来获取用户列表,但它没有获得Active Directory中的所有用户



 尝试 
{

string domainAndUsername = domain + @ \ + admin;

DirectoryEntry myLdapConnection = new DirectoryEntry(LdapPath,domainAndUsername,passwrd);
DirectorySearcher search = new DirectorySearcher(myLdapConnection){Filter =( (objectClass = user))};

search.CacheResults = true ;
SearchResultCollection allResults = search.FindAll();
DataTable resultsTable = new DataTable();
resultsTable.Columns.Add( UserID);
resultsTable.Columns.Add( EmailID);
foreach (SearchResult searchResult in allResults)
{



MembershipUser myUser = Membership.GetAllUsers()[searchResult.Properties [ sAMAccountName ] [ 0 ]。ToString()];

if (myUser == null
{
DataRow dr = resultsTable.NewRow();
dr [ UserID] = searchResult.Properties [ SAMAccountName] [ 0 ]。ToString();
if (searchResult.Properties [ mail ]。计数> 0
{
dr [ EmailID] = searchResult.Properties [ mail] [ 0 ]。ToString();
}
其他
{
dr [ EmailID] = ;
}
resultsTable.Rows.Add(dr);
}
else
{}
}
grdViewAllADSUsers.DataSource = resultsTable;
grdViewAllADSUsers.DataBind();


}
catch (例外)
{
}

解决方案

可能用户没有填充,因为在您的数据表上添加了用户不执行的acceptchanges操作后。



之后(resultsTable.Rows.Add(dr))





所以请用这一行更新上面的代码代码(听我的假设是用户在searchresults(和AllResults)对象中正确填充,并且在运行时显示数据有问题。



 尝试 
{

string domainAndUsername = domain + @ \ + adm在;

DirectoryEntry myLdapConnection = new DirectoryEntry(LdapPath,domainAndUsername,passwrd);
DirectorySearcher search = new DirectorySearcher(myLdapConnection){Filter =( (objectClass = user))};

search.CacheResults = true ;
SearchResultCollection allResults = search.FindAll();
DataTable resultsTable = new DataTable();
resultsTable.Columns.Add( UserID);
resultsTable.Columns.Add( EmailID);
foreach (SearchResult searchResult in allResults)
{



MembershipUser myUser = Membership.GetAllUsers()[searchResult.Properties [ sAMAccountName ] [ 0 ]。ToString()];

if (myUser == null
{
DataRow dr = resultsTable.NewRow();
dr [ UserID] = searchResult.Properties [ SAMAccountName] [ 0 ]。ToString();
if (searchResult.Properties [ mail ]。计数> 0
{
dr [ EmailID] = searchResult.Properties [ mail] [ 0 ]。ToString();
}
其他
{
dr [ EmailID] = ;
}
resultsTable.Rows.Add(dr);
}
其他
{}
}
/ // 可能的解决方案

resultstable.AcceptChanges();

grdViewAllADSUsers.DataSource = resultsTable;
grdViewAllADSUsers.DataBind();


}
catch (例外)
{
}


i have to get list of users in a group of Active directory i have different group when user select the group name itshould fill the list of users belog to that group in gridview

i am using below code for getting the list of user but its not getting all the users in Active Directory

try
       {

           string domainAndUsername = "domain" + @"\" + "admin";

           DirectoryEntry myLdapConnection = new DirectoryEntry(LdapPath, domainAndUsername, passwrd);
           DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ("(objectClass=user)") };

           search.CacheResults = true;
           SearchResultCollection allResults = search.FindAll();
           DataTable resultsTable = new DataTable();
           resultsTable.Columns.Add("UserID");
           resultsTable.Columns.Add("EmailID");
           foreach (SearchResult searchResult in allResults)
           {



               MembershipUser myUser = Membership.GetAllUsers()[searchResult.Properties["sAMAccountName"][0].ToString()];

               if (myUser == null)
               {
                   DataRow dr = resultsTable.NewRow();
                   dr["UserID"] = searchResult.Properties["SAMAccountName"][0].ToString();
                   if (searchResult.Properties["mail"].Count > 0)
                   {
                       dr["EmailID"] = searchResult.Properties["mail"][0].ToString();
                   }
                   else
                   {
                       dr["EmailID"] = "";
                   }
                   resultsTable.Rows.Add(dr);
               }
               else
               { }
           }
           grdViewAllADSUsers.DataSource = resultsTable;
           grdViewAllADSUsers.DataBind();


       }
       catch (Exception)
       {
       }

解决方案

Probably users are not populating because after adding user your not performing acceptchanges operation on your datatable.

after (resultsTable.Rows.Add(dr))


so please update above code with this lines of code (Hear my assumption is that users are populated in searchresults (and AllResults) objects properly and problem with displaying data at runtime.

try
        {
            
            string domainAndUsername = "domain" + @"\" + "admin";
           
            DirectoryEntry myLdapConnection = new DirectoryEntry(LdapPath, domainAndUsername, passwrd);
            DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ("(objectClass=user)") };
        
            search.CacheResults = true;
            SearchResultCollection allResults = search.FindAll();
            DataTable resultsTable = new DataTable();
            resultsTable.Columns.Add("UserID");
            resultsTable.Columns.Add("EmailID");
            foreach (SearchResult searchResult in allResults)
            {
 
               
 
                MembershipUser myUser = Membership.GetAllUsers()[searchResult.Properties["sAMAccountName"][0].ToString()];
            
                if (myUser == null)
                {
                    DataRow dr = resultsTable.NewRow();
                    dr["UserID"] = searchResult.Properties["SAMAccountName"][0].ToString();
                    if (searchResult.Properties["mail"].Count > 0)
                    {
                        dr["EmailID"] = searchResult.Properties["mail"][0].ToString();
                    }
                    else
                    {
                        dr["EmailID"] = "";
                    }
                    resultsTable.Rows.Add(dr);
                }
                else
                { }
            }
               ///probable solution

            resultstable.AcceptChanges();

            grdViewAllADSUsers.DataSource = resultsTable;
            grdViewAllADSUsers.DataBind();
          
 
        }
        catch (Exception)
        {
        }


这篇关于如何只获取一组Active Directory中的用户列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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