AD(Active Directory)以不同的数据源显示数据的实现(包含源代码)
Active Directory 是一种企业级目录服务,该服务可伸缩、使用 Internet 标准技术从基础建立,并完全在操作系统级别上集成。Active Directory 简化了管理,使用户很容易找到各种资源。Active Directory 提供了非常广泛的特性和功能。
一、单一数据源的实现
下面代码实现了取得AD里面的数据,并返回一个DataSet
二、多数据源的实现
首先定义一个IDataSource接口,让不同的数据源实现此接口
整个解决方案如下图所示:
运行结果如下图所示:
源代码下载:
地址一: http://files.cnblogs.com/binbin1845/ADApplication.rar
地址二: http://download.csdn.net/source/304545
一、单一数据源的实现
下面代码实现了取得AD里面的数据,并返回一个DataSet
public class ADHelper
{
public static DataSet GetAllAccounts(string adAddress, string account, string password)
{
DirectoryEntry entry = new DirectoryEntry(adAddress, account, password);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(objectClass=user)";
mySearcher.SearchScope = SearchScope.Subtree;
DataTable dt = MakeTable();
DataRow dr;
try
{
SearchResultCollection results = mySearcher.FindAll();
foreach (SearchResult result in results)
{
dr = dt.NewRow();
foreach (DictionaryEntry de in GetADInfo())
{
dr[de.Key.ToString()] = GetProperty(result, de.Value.ToString());
}
dt.Rows.Add(dr);
}
}
catch
{
throw new Exception("Can't connect to Active Directory.");
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
private static string GetProperty(SearchResult searchResult, string PropertyName)
{
if(searchResult.Properties.Contains(PropertyName))
{
return searchResult.Properties[PropertyName][0].ToString() ;
}
else
{
return string.Empty;
}
}
private static DataColumn MakeColumn(string name)
{
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = name;
return column;
}
private static DataTable MakeTable()
{
DataTable adTable = new DataTable();
foreach (DictionaryEntry de in GetADInfo())
{
adTable.Columns.Add(MakeColumn(de.Key.ToString()));
}
return adTable;
}
private static Hashtable GetADInfo()
{
Hashtable table = new Hashtable();
table.Add("Account", "cn");
table.Add("FirstName", "givenName");
table.Add("LastName", "sn");
table.Add("Company", "company");
table.Add("PhoneNumber", "telephoneNumber");
table.Add("Fax", "facsimileTelephoneNumber");
table.Add("Email", "mail");
table.Add("Address", "homePostalAddress");
table.Add("ZipCode", "postalCode");
return table;
}
}
然后调用此方法:{
public static DataSet GetAllAccounts(string adAddress, string account, string password)
{
DirectoryEntry entry = new DirectoryEntry(adAddress, account, password);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(objectClass=user)";
mySearcher.SearchScope = SearchScope.Subtree;
DataTable dt = MakeTable();
DataRow dr;
try
{
SearchResultCollection results = mySearcher.FindAll();
foreach (SearchResult result in results)
{
dr = dt.NewRow();
foreach (DictionaryEntry de in GetADInfo())
{
dr[de.Key.ToString()] = GetProperty(result, de.Value.ToString());
}
dt.Rows.Add(dr);
}
}
catch
{
throw new Exception("Can't connect to Active Directory.");
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
private static string GetProperty(SearchResult searchResult, string PropertyName)
{
if(searchResult.Properties.Contains(PropertyName))
{
return searchResult.Properties[PropertyName][0].ToString() ;
}
else
{
return string.Empty;
}
}
private static DataColumn MakeColumn(string name)
{
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = name;
return column;
}
private static DataTable MakeTable()
{
DataTable adTable = new DataTable();
foreach (DictionaryEntry de in GetADInfo())
{
adTable.Columns.Add(MakeColumn(de.Key.ToString()));
}
return adTable;
}
private static Hashtable GetADInfo()
{
Hashtable table = new Hashtable();
table.Add("Account", "cn");
table.Add("FirstName", "givenName");
table.Add("LastName", "sn");
table.Add("Company", "company");
table.Add("PhoneNumber", "telephoneNumber");
table.Add("Fax", "facsimileTelephoneNumber");
table.Add("Email", "mail");
table.Add("Address", "homePostalAddress");
table.Add("ZipCode", "postalCode");
return table;
}
}
DataSet ds = ADHelper.GetAllAccounts("LDAP://192.168.3.23", "sa", "sa");
dataGridView1.DataSource = ds.Tables[0].DefaultView;
如果需要增加一个数据列,比如Job,只需要增加一行代码就可以了dataGridView1.DataSource = ds.Tables[0].DefaultView;
table.Add("Job", "extensionAttribute8");
如果目前的需求只是得到AD中的数据,该数据以GridView的形式显示出来,这种方法还是可行的。但是一旦需求变化,改为AD中的数据显示在一个ListBox中,或者以一个树形结构显示出来,那么就需要增加两个方法public static ArrayList GetAllAccounts(string adAddress, string account, string password)和public static TreeNode GetAllAccounts(string adAddress, string account, string password),如果还需要其他的形式显示,这样的代码还需要修改这个ADHelp类,造成了代码大量冗余,代码很难进行维护。二、多数据源的实现
首先定义一个IDataSource接口,让不同的数据源实现此接口
public interface IDataSource
{
void AddLine(UserInfo user);
object GetData();
}
Grid数据源的实现{
void AddLine(UserInfo user);
object GetData();
}
public class GridDataSource : IDataSource
{
DataTable dt;
public GridDataSource()
{
dt = MakeTable();
}
public void AddLine(UserInfo user)
{
DataRow dr;
dr = dt.NewRow();
dr[UserInfo.ACCOUNT] = user.Account;
dr[UserInfo.FIRSTNAME] = user.FirstName;
dr[UserInfo.LASTNAME] = user.LastName;
dr[UserInfo.EMAIL] = user.Email;
dt.Rows.Add(dr);
}
public object GetData()
{
return dt;
}
private DataColumn MakeColumn(string name)
{
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = name;
return column;
}
private DataTable MakeTable()
{
DataTable dTable = new DataTable();
dTable.Columns.Add(MakeColumn(UserInfo.ACCOUNT));
dTable.Columns.Add(MakeColumn(UserInfo.FIRSTNAME));
dTable.Columns.Add(MakeColumn(UserInfo.LASTNAME));
dTable.Columns.Add(MakeColumn(UserInfo.EMAIL));
return dTable;
}
}
List数据源的实现{
DataTable dt;
public GridDataSource()
{
dt = MakeTable();
}
public void AddLine(UserInfo user)
{
DataRow dr;
dr = dt.NewRow();
dr[UserInfo.ACCOUNT] = user.Account;
dr[UserInfo.FIRSTNAME] = user.FirstName;
dr[UserInfo.LASTNAME] = user.LastName;
dr[UserInfo.EMAIL] = user.Email;
dt.Rows.Add(dr);
}
public object GetData()
{
return dt;
}
private DataColumn MakeColumn(string name)
{
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = name;
return column;
}
private DataTable MakeTable()
{
DataTable dTable = new DataTable();
dTable.Columns.Add(MakeColumn(UserInfo.ACCOUNT));
dTable.Columns.Add(MakeColumn(UserInfo.FIRSTNAME));
dTable.Columns.Add(MakeColumn(UserInfo.LASTNAME));
dTable.Columns.Add(MakeColumn(UserInfo.EMAIL));
return dTable;
}
}
public class ListDataSource : IDataSource
{
ArrayList list;
public ListDataSource()
{
list = new ArrayList();
}
public void AddLine(UserInfo user)
{
list.Add(user.Account + "----" + user.FirstName + "----" + user.LastName + "----" + user.Email);
}
public object GetData()
{
return list;
}
}
Tree数据源的实现{
ArrayList list;
public ListDataSource()
{
list = new ArrayList();
}
public void AddLine(UserInfo user)
{
list.Add(user.Account + "----" + user.FirstName + "----" + user.LastName + "----" + user.Email);
}
public object GetData()
{
return list;
}
}
public class TreeDataSource : IDataSource
{
TreeNode node;
public TreeDataSource()
{
node = new TreeNode();
node.Text = "Users";
node.ExpandAll();
}
public void AddLine(UserInfo user)
{
TreeNode newNode = new TreeNode();
newNode.Text = user.Account;
newNode.Nodes.Add(user.FirstName);
newNode.Nodes.Add(user.LastName);
newNode.Nodes.Add(user.Email);
newNode.ExpandAll();
node.Nodes.Add(newNode);
}
public object GetData()
{
return node;
}
}
然后,我们在定义一个类来管理这些数据源{
TreeNode node;
public TreeDataSource()
{
node = new TreeNode();
node.Text = "Users";
node.ExpandAll();
}
public void AddLine(UserInfo user)
{
TreeNode newNode = new TreeNode();
newNode.Text = user.Account;
newNode.Nodes.Add(user.FirstName);
newNode.Nodes.Add(user.LastName);
newNode.Nodes.Add(user.Email);
newNode.ExpandAll();
node.Nodes.Add(newNode);
}
public object GetData()
{
return node;
}
}
public class ADManager
{
string adAddress;
string account;
string password;
public IList<UserInfo> users;
public ADManager(string adAddress, string account, string password)
{
this.adAddress = adAddress;
this.account = account;
this.password = password;
GetUserData();
}
private IList<UserInfo> GetUserData()
{
users = new List<UserInfo>();
DirectoryEntry entry = new DirectoryEntry(adAddress, account, password);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(objectClass=user)";
mySearcher.SearchScope = SearchScope.Subtree;
try
{
SearchResultCollection results = mySearcher.FindAll();
UserInfo user;
foreach (SearchResult result in results)
{
user = new UserInfo();
user.Account = GetProperty(result, UserInfo.ACCOUNT_AD);
user.FirstName = GetProperty(result, UserInfo.FIRSTNAME_AD);
user.LastName = GetProperty(result, UserInfo.LASTNAME_AD);
user.Email = GetProperty(result, UserInfo.EMAIL_AD);
users.Add(user);
}
}
catch
{
throw new Exception("Can't connect to Active Directory.");
}
return users;
}
public object GetDataSource(DataSourceType type)
{
DataSourceFactory factory = new DataSourceFactory();
IDataSource source = factory.CreateDataSource(type);
foreach (UserInfo user in users)
{
source.AddLine(user);
}
return source.GetData();
}
}
客户端访问也很简单,只需要知道访问的类型就行了{
string adAddress;
string account;
string password;
public IList<UserInfo> users;
public ADManager(string adAddress, string account, string password)
{
this.adAddress = adAddress;
this.account = account;
this.password = password;
GetUserData();
}
private IList<UserInfo> GetUserData()
{
users = new List<UserInfo>();
DirectoryEntry entry = new DirectoryEntry(adAddress, account, password);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(objectClass=user)";
mySearcher.SearchScope = SearchScope.Subtree;
try
{
SearchResultCollection results = mySearcher.FindAll();
UserInfo user;
foreach (SearchResult result in results)
{
user = new UserInfo();
user.Account = GetProperty(result, UserInfo.ACCOUNT_AD);
user.FirstName = GetProperty(result, UserInfo.FIRSTNAME_AD);
user.LastName = GetProperty(result, UserInfo.LASTNAME_AD);
user.Email = GetProperty(result, UserInfo.EMAIL_AD);
users.Add(user);
}
}
catch
{
throw new Exception("Can't connect to Active Directory.");
}
return users;
}
public object GetDataSource(DataSourceType type)
{
DataSourceFactory factory = new DataSourceFactory();
IDataSource source = factory.CreateDataSource(type);
foreach (UserInfo user in users)
{
source.AddLine(user);
}
return source.GetData();
}
}
ADManager ad = new ADManager("LDAP://192.168.3.27", "sa", "sa");
dataGridView1.DataSource = ad.GetDataSource(DataSourceType.Grid);
listBox1.DataSource = ad.GetDataSource(DataSourceType.List);
treeView1.Nodes.Add((TreeNode)ad.GetDataSource(DataSourceType.Tree));
dataGridView1.DataSource = ad.GetDataSource(DataSourceType.Grid);
listBox1.DataSource = ad.GetDataSource(DataSourceType.List);
treeView1.Nodes.Add((TreeNode)ad.GetDataSource(DataSourceType.Tree));
整个解决方案如下图所示:
运行结果如下图所示:
源代码下载:
地址一: http://files.cnblogs.com/binbin1845/ADApplication.rar
地址二: http://download.csdn.net/source/304545
转载于:https://www.cnblogs.com/binbin1845/archive/2007/12/10/989514.html