假设AD中有一组织单位,给定以下信息:
AD:ms.com
AD管理员:administrator
AD管理员密码:pass@word1
组织单位名称:XX有限公司(不必理会其下有嵌套多少组织单位,通常都是部门)
现在要获取这一组织单位下的所有的用户信息,比如只要:帐号,姓名,邮件,所在组织这四个字段,具体实现如示例代码所示:
AD:ms.com
AD管理员:administrator
AD管理员密码:pass@word1
组织单位名称:XX有限公司(不必理会其下有嵌套多少组织单位,通常都是部门)
现在要获取这一组织单位下的所有的用户信息,比如只要:帐号,姓名,邮件,所在组织这四个字段,具体实现如示例代码所示:
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;
}
}
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;
}
}