求一个用asp.net从AD获取所有用户信息的程序,并且把所有信息 都写入一个Dataset.。100分
求一个用asp.net从AD获取所有用户信息的程序,并且把所有信息 都写入一个Dataset.
------解决方案--------------------贴代码吧,以前网上下的,没AD环境测试
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Principal;
using System.DirectoryServices;
using System.Collections.Specialized;
namespace AD.Net
{
public partial class frmADTree : Form
{
public frmADTree()
{
InitializeComponent();
}
private void frmADTree_Load(object sender, EventArgs e)
{
string LogonName = GetLogonName();
txtAD.Text = "LDAP:// " + LogonName.Split( '\\ ')[0];
txtUserName.Text = LogonName.Split( '\\ ')[1];
}
private string GetLogonName()
{
return System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}
private void button1_Click(object sender, EventArgs e)
{
/// 1. objectClass=organizationalUnit 查询条件是所有的组织单元(OU)
/// 2. objectClass=group 查询条件是所有的组(GROUP)
/// 3. objectClass=user 查询条件是所有的用户(USER)
string schemaClassNameToSearch = "organizationalUnit ";
// 得到组织结构树的字符串数组
string[] ADOU = GetOrganizationalUnit(txtAD.Text, txtUserName.Text, txtPwd.Text, schemaClassNameToSearch);
schemaClassNameToSearch = "user ";
// 得到用户的信息,包括“组织名,登录名,用户名,邮件地址”用“,”进行分隔
string[] ADUser = GetUserUnit(txtAD.Text, txtUserName.Text, txtPwd.Text, schemaClassNameToSearch);
foreach (string s in ADOU)
{
TreeNode node = new TreeNode(s);
tree.Nodes.Add(node);
AddUser(ADUser, node);
}
}
private void AddUser(string[] ADUser, TreeNode node)
{
if (ADUser.Length <= 0)
throw new Exception( "域中没有任何用户 ");
if (node == null)
throw new ArgumentNullException( "node ");
foreach (string s in ADUser)
{
string[] sSplit = s.Split( ', ');
if (sSplit.Length > 0)
{
if (sSplit[0] == node.Text)
{
TreeNode childrenNode = new TreeNode(sSplit[2]);
node.Nodes.Add(childrenNode);
}
}
}
}
private string[] GetUserUnit(string domainADsPath, string username, string password, string schemaClassNameToSearch)
{
SearchResultCollection results = _ADHelper(domainADsPath, username, password, schemaClassNameToSearch);
string[] sRe = GetGetUserUnitResults(results);
results.Dispose();
return sRe;
}
private string[] GetGetUserUnitResults(SearchResultCollection results)
{
string sRe = string.Empty;
if (results.Count == 0)
throw new Exception( "域中没有任何用户 ");
else
{
foreach (Sear