首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

用asp.net从AD获取所有用户信息的程序,并且把所有信息 都写入一个Dataset.100分

2012-01-18 
求一个用asp.net从AD获取所有用户信息的程序,并且把所有信息 都写入一个Dataset.。100分求一个用asp.net从A

求一个用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 (SearchResult result in results)
{
string adPath = result.Path;


if (adPath.IndexOf( "OU=用户 ") < 0)
continue;
//“组织名,登录名,用户名,邮件地址”
// 分解Path得到组织名
string[] sSplit = adPath.Split( ', ');
string orz = string.Empty;
if (sSplit.Length > 2)
{
orz = sSplit[1].Split( '= ')[1];
}
ResultPropertyCollection propColl = result.Properties;
if (propColl[ "samaccountname "].Count > 0)
orz += ", " + propColl[ "samaccountname "][0].ToString();
if (propColl[ "name "].Count > 0)
orz += ", " + propColl[ "name "][0].ToString();
if(propColl[ "mail "].Count > 0)
orz += ", " + propColl[ "mail "][0].ToString();
sRe += orz + "= ";
}
}
if (sRe.Length > 0)
sRe = sRe.Substring(0, sRe.Length - 1);
return sRe.Split( '= ');
}



private string[] GetOrganizationalUnit(string domainADsPath, string username, string password,string schemaClassNameToSearch)
{
SearchResultCollection results = _ADHelper(domainADsPath, username, password, schemaClassNameToSearch);
string[] sRe = GetGetOrganizationalUnitResults(results);

results.Dispose();

return sRe;
}

private static SearchResultCollection _ADHelper(string domainADsPath, string username, string password, string schemaClassNameToSearch)
{
DirectorySearcher searcher = new DirectorySearcher();

searcher.SearchRoot = new DirectoryEntry(domainADsPath,
username, password);
searcher.Filter = "(objectClass= " + schemaClassNameToSearch + ") ";

searcher.SearchScope = SearchScope.Subtree;
searcher.Sort = new SortOption( "name ",
SortDirection.Ascending);
searcher.PageSize = 512;

searcher.PropertiesToLoad.AddRange(new string[] { "name ", "Path ", "displayname ", "samaccountname ", "mail " });

SearchResultCollection results = searcher.FindAll();
return results;
}

private string[] GetGetOrganizationalUnitResults(SearchResultCollection results)
{
string sRe = string.Empty;
if (results.Count == 0)
throw new Exception( "域中没有任何组织结构 ");
else
{
foreach (SearchResult result in results)
{
if (result.Path.IndexOf( "OU=用户 ") < 0)
continue;
ResultPropertyCollection propColl = result.Properties;
sRe += propColl[ "name "][0].ToString() + ', ';
}
}
if (sRe.Length > 0)
sRe = sRe.Substring(0, sRe.Length - 1);
return sRe.Split( ', ');
}

}
}
[解决办法]


其实没那么复杂
楼上那位还有生成tree的一系列操作

热点排行