資源簡介
C#讀取AD域組織架構及用戶例子。值得初學者一看。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.DirectoryServices;
using?System.Collections;
using?System.Text.Regularexpressions;
namespace?ActiveDirectoryDome
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????if?(TryAuthenticate(“whjh.com“?“lity“?“admins“))
????????????{
????????????????Console.WriteLine(“登陸成功!“);
????????????}
????????????else
????????????{
????????????????Console.WriteLine(“登陸失敗!“);
????????????}
????????????Console.WriteLine(“*******************************************************“);
????????????List?group?=?new?List();
????????????group?=?GetADGroups(“lity“?“whjh.com“?“lity“?“admins“);
????????????foreach?(string?temp?in?group)
????????????{
????????????????Console.WriteLine(“組名稱:“+temp.ToString());
????????????}
????????????Console.ReadLine();
????????}
????????///?
????????///?驗證AD用戶是否登陸成功
????????///?
????????///?域名稱
????????///?用戶名
????????///?密碼
????????///?返回登陸狀態
????????public?static?bool?TryAuthenticate(string?domain?string?username?string?password)
????????{
????????????bool?isLogin?=?false;
????????????try
????????????{
????????????????DirectoryEntry?entry?=?new?DirectoryEntry(string.Format(“LDAP://{0}“?domain)?username?password);
????????????????entry.RefreshCache();
????????????????isLogin?=?true;
????????????}
????????????catch
????????????{
????????????????isLogin?=?false;
????????????}
????????????return?isLogin;
????????}
????????///?
????????///?取用戶所對應的用戶組
????????///?
????????///?用戶名稱
????????///?域
????????///?登陸用戶
????????///?登陸密碼
????????///?
????????public?static?List?GetADGroups(string?userNamestring?domainstring?adusernamestring?adpassword)
????????{
????????????List?groups?=?new?List();
????????????try
????????????{
????????????????var?entry?=?new?DirectoryEntry(string.Format(“LDAP://{0}“?domain)?adusername?adpassword);
????????????????entry.RefreshCache();
????????????????DirectorySearcher?search?=?new?DirectorySearcher(entry);
????????????????//search.PropertiesToLoad.Add(“memberof“);
????????????????//search.Filter?=?string.Format(“sAMAccountName={0}“?userName);
????????????????//SearchResult?result?=?search.FindOne();
????????????????search.Filter?=?“(&(objectCategory=person)(objectClass=user))“;
????????????????search.Sort?=?new?SortOption(“samaccountname“?SortDirection.Ascending);
????????????????search.SearchScope?=?SearchScope.Subtree;
????????????????SearchResultCollection?resultColl?=?search.FindAll();
????????????????foreach?(SearchResult?result?in?resultColl)
???????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2565??2010-08-17?11:06??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\ActiveDirectoryDome.csproj
?????文件???????7168??2011-11-24?13:45??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\bin\Debug\ActiveDirectoryDome.exe
?????文件??????15872??2011-11-24?13:45??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\bin\Debug\ActiveDirectoryDome.pdb
?????文件??????11592??2011-11-24?13:45??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\bin\Debug\ActiveDirectoryDome.vshost.exe
?????文件????????490??2009-06-11?05:14??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\bin\Debug\ActiveDirectoryDome.vshost.exe.manifest
?????文件???????1736??2011-11-24?13:45??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\obj\Debug\ActiveDirectoryDome.csproj.FileListAbsolute.txt
?????文件???????7168??2011-11-24?13:45??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\obj\Debug\ActiveDirectoryDome.exe
?????文件??????15872??2011-11-24?13:45??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\obj\Debug\ActiveDirectoryDome.pdb
?????文件???????3818??2011-11-24?13:30??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\Program.cs
?????文件???????1370??2010-08-17?10:37??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\Properties\AssemblyInfo.cs
?????文件???????1434??2011-11-24?13:44??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome.sln
????..A..H.?????29184??2011-11-24?13:48??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome.suo
?????文件???????4269??2011-11-24?13:44??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\ADYUAccount.csproj
?????文件???????1297??2011-11-24?13:44??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\ADYUAccount.csproj.user
?????文件????????389??2011-11-24?14:05??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\ADYUAccount.Publish.xm
?????文件???????8192??2011-11-24?14:14??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\bin\ADYUAccount.dll
?????文件??????17920??2011-11-24?14:14??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\bin\ADYUAccount.pdb
?????文件????????813??2011-11-24?13:54??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\Default.aspx
?????文件???????8720??2011-11-24?14:14??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\Default.aspx.cs
?????文件???????2560??2011-11-24?13:47??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\Default.aspx.designer.cs
?????文件????????498??2011-11-24?13:48??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\obj\Debug\ADYUAccount.csproj.FileListAbsolute.txt
?????文件???????8192??2011-11-24?14:14??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\obj\Debug\ADYUAccount.dll
?????文件??????17920??2011-11-24?14:14??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\obj\Debug\ADYUAccount.pdb
?????文件???????1309??2011-11-24?13:35??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\Properties\AssemblyInfo.cs
?????文件???????7305??2011-11-24?13:45??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\Web.config
?????目錄??????????0??2010-08-17?11:41??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\obj\Debug\TempPE
?????目錄??????????0??2011-11-24?13:35??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\obj\Debug\TempPE
?????目錄??????????0??2011-11-23?17:57??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\bin\Debug
?????目錄??????????0??2011-11-24?13:45??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ActiveDirectoryDome\obj\Debug
?????目錄??????????0??2011-11-24?14:14??C#讀取AD域里用戶名或組\dome\ActiveDirectoryDome\ADYUAccount\obj\Debug
............此處省略15個文件信息
評論
共有 條評論