C# AD(Active Directory)域信息同步,組織單位、用戶等信息查詢
C# AD(Active Directory)域信息同步,組織單位、用戶等信息查詢 接上篇 Windows Server 2008 R2 配置AD(Active Directory)域控制器 對(duì)AD
C# AD(Active Directory)域信息同步,組織單位、用戶等信息查詢 接上篇 Windows Server 2008 R2 配置AD(Active Directory)域控制器 對(duì)AD 域結(jié)合常見(jiàn)需求用

C#進(jìn)行一些讀取信息的操作^_^!
?
?
?
?
? 示例準(zhǔn)備 知識(shí)了解 讀取AD 域信息示例 DirectorySearcher.Filter 屬性擴(kuò)充說(shuō)明 用戶屬性擴(kuò)充說(shuō)明(含圖文屬性對(duì)照)
?
?
?
?
?
?
常規(guī) 地址

帳戶 電話 組織 示例下載
,


新建層次關(guān)系如下:
,
下面我們開(kāi)始連接域,并讀取出示例準(zhǔn)備中鍵好的組織單位和用戶 首先編寫(xiě)代碼用

LDAP 嘗試對(duì)域進(jìn)行訪問(wèn)
形式:LDAP://Domain
#region## 是否連接到域
///
/// 功能:是否連接到域
/// 作者:Wilson
/// 時(shí)間:2012-12-15
///
///
/// 域名或IP
/// 用戶名
/// 密碼
/// 域
///
private bool IsConnected(string domainName, string userName, string userPwd, out DirectoryEntry domain)
{
domain = new DirectoryEntry();
try
{
domain.Path = string.Format("LDAP://{0}", domainName);
domain.Username = userName;
domain.Password = userPwd;
domain.AuthenticationType = AuthenticationTypes.Secure;
domain.RefreshCache();
return true;
}
catch(Exception ex)
,{
LogRecord.WriteLog("[IsConnected方法]錯(cuò)誤信息:" ex.Message);
return false;
}
} #endregion



傳用參數(shù),調(diào)IsConnected 方法,結(jié)果如下
連接上AD 域后,接著我們找到根OU
#region## 域中是否存在組織單位
///
/// 功能:域中是否存在組織單位
/// 作者:Wilson
/// 時(shí)間:2012-12-15
///
///
///
///
private bool IsExistOU(DirectoryEntry entry, out DirectoryEntry ou)
{
ou = new DirectoryEntry();
try
{
ou = entry.Children.Find("OU=" txtRootOU.Text.Trim());
return (ou != null);
,}
catch(Exception ex)
{
LogRecord.WriteLog("[IsExistOU方法]錯(cuò)誤信息:" ex.Message);
return false;
}
} #endregion



傳入以數(shù),調(diào)用IsExistOU 方法,結(jié)果如下
下面來(lái)開(kāi)始讀取組織單位及用戶的信息。
示例為了看出層次關(guān)系及導(dǎo)出信息是類型區(qū)分,給OU 和User 新建了一個(gè)實(shí)體類和一個(gè)類型的枚舉
#region## 類型
///
/// 類型
///
public enum TypeEnum : int
{
///
/// 組織單位
///
OU = 1,
///
/// 用戶
,///
USER = 2 }

#endregion

#region## Ad域信息實(shí)體
///
/// Ad域信息實(shí)體
///
public class AdModel
{
public AdModel(string id, string name, int typeId, string parentId) {
Id = id;
Name = name;
TypeId = typeId;
ParentId = parentId;
}
public string Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
public string ParentId { get; set; }
}
#endregion
下面讀取信息
private List
#region## 同步
///
/// 功能:同步
/// 創(chuàng)建人:Wilson
/// 創(chuàng)建時(shí)間:2012-12-15
///
///
public void SyncAll(DirectoryEntry entryOU)
{
DirectorySearcher mySearcher = new DirectorySearcher(entryOU, "(objectclass=organizationalUnit)"); //查詢組織單位
,DirectoryEntry root = mySearcher.SearchRoot; //查找根OU
SyncRootOU(root);
StringBuilder sb = new StringBuilder();
sb.Append("rnIDt帳號(hào)t類型t父IDrn");
foreach (var item in list)
{
sb.AppendFormat("{0}t{1}t{2}t{3}rn", item.Id, item.Name, item.TypeId, item.ParentId);
}
LogRecord.WriteLog(sb.ToString());
MessageBox.Show("同步成功", this.Text, MessageBoxButtons.OK,
MessageBoxIcon.Information);
Application.Exit();
}
#endregion
#region## 同步根組織單位
///
/// 功能: 同步根組織單位
/// 創(chuàng)建人:Wilson
/// 創(chuàng)建時(shí)間:2012-12-15
///
///
private void SyncRootOU(DirectoryEntry entry)
{
if (entry.Properties.Contains("ou") &&
entry.Properties.Contains("objectGUID"))
{
string rootOuName = entry.Properties["ou"][0].ToString();
byte[] bGUID = entry.Properties["objectGUID"][0] as byte[];
string id = BitConverter.ToString(bGUID);
list.Add(new AdModel(id, rootOuName, (int)TypeEnum.OU, "0"));
,SyncSubOU(entry, id);
}
}
#endregion
#region## 同步下屬組織單位及下屬用戶
///
/// 功能: 同步下屬組織單位及下屬用戶
/// 創(chuàng)建人:Wilson
/// 創(chuàng)建時(shí)間:2012-12-15
///
///
///
private void SyncSubOU(DirectoryEntry entry, string parentId)
{
foreach (DirectoryEntry subEntry in entry.Children)
{
string entrySchemaClsName = subEntry.SchemaClassName;
string[] arr = subEntry.Name.Split('=');
string categoryStr = arr[0];
string nameStr = arr[1];
string id = string.Empty;
if (subEntry.Properties.Contains("objectGUID")) //SID
{
byte[] bGUID = subEntry.Properties["objectGUID"][0] as byte[];
id = BitConverter.ToString(bGUID);
}
bool isExist = list.Exists(d => d.Id == id);
switch (entrySchemaClsName)
{
case "organizationalUnit":
if (!isExist)
{
list.Add(new AdModel(id, nameStr, (int)TypeEnum.OU, parentId)); }
SyncSubOU(subEntry, id);
break;
,case "user":
string accountName = string.Empty;
if (subEntry.Properties.Contains("samaccountName"))
{
accountName =
subEntry.Properties["samaccountName"][0].ToString();
}

if (!isExist)

{

list.Add(new AdModel(id, accountName, (int)TypeEnum.USER,

parentId));
}
break;
}
}
}
#endregion
調(diào)用SyncAll 方法循環(huán)輸出list, 結(jié)果如下, 很清楚的可以看出層次關(guān)系
//ID 帳號(hào) 類型 父ID
//58-D6-C4-32-6A-A1-99-48-A4-8B-C8-5D-BC-C9-3E-17 acompany 1 0 //FB-44-91-AE-AC-73-2B-4D-9F-01-B1-E2-16-D3-CB-1B department01 1 58-D6-C4-32-6A-A1-99-48-A4-8B-C8-5D-BC-C9-3E-17
//47-9D-5B-91-60-22-D1-46-B0-CD-C7-B2-C7-D3-00-31 department03 1 FB-44-91-AE-AC-73-2B-4D-9F-01-B1-E2-16-D3-CB-1B
//E3-AD-47-45-38-64-02-4D-B9-83-2C-50-67-50-4F-92 zw 2 47-9D-5B-91-60-22-D1-46-B0-CD-C7-B2-C7-D3-00-31
//8A-D4-23-18-F3-6F-E1-47-93-7A-CC-07-76-4B-E7-86 zhongw 2 FB-44-91-AE-AC-73-2B-4D-9F-01-B1-E2-16-D3-CB-1B
//BC-D0-34-85-67-2F-05-4D-B5-77-E3-F4-AD-51-45-02 department02 1 58-D6-C4-32-6A-A1-99-48-A4-8B-C8-5D-BC-C9-3E-17
//1C-13-FA-66-E4-51-65-49-8B-DC-22-60-32-34-8F-22 wilson 2 BC-D0-34-85-67-2F-05-4D-B5-77-E3-F4-AD-51-45-02
//84-E8-E5-9A-6B-56-E2-45-9A-87-54-D1-78-6B-D3-56 porschev 2 58-D6-C4-32-6A-A1-99-48-A4-8B-C8-5D-BC-C9-3E-17
,

常項(xiàng)選項(xiàng)卡