📄 adhelper.cs
字号:
/*
* Author : 方伍
* Date: 2007-12-10
* Blog: http://www.cnblogs.com/binbin1845
* QQ: 32418201
* Email: binbin1845@126.com
*/
using System;
using System.Data;
using System.Collections;
using System.DirectoryServices;
namespace ADApplication
{
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");
table.Add("Job", "extensionAttribute8");
return table;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -