2011-04-22 155 views
1

我發現了大約從Active Directory獲取用戶的一篇文章從here連接到Active Directory的使用C#

所以可能我的代碼會喜歡這個

String strPath = "format of this path"; 
DirectoryEntry entry = null; 
entry = new DirectoryEntry(strPath); 

DirectorySearcher mySearcher = new DirectorySearcher(entry); 

mySearcher.Filter = ("ObjectCategory=user"); 

foreach (SearchResult result in mySearcher.FindAll()) 
{ 
    String strName = result.GetDirectoryEntry().Name; 
    //Do whatever 
} 

能否請您在此解釋一下strPath中檢索用戶的詳細信息??這是什麼格式?

注意:我知道我的服務器信息可以使用我的本地ip「198.168.1.182」進行測試。

我不確定我的想法是否正確。

請幫忙!!!

回答

5

由於您使用的是.NET 4,因此您一定要查看System.DirectoryServices.AccountManagement(S.DS.AM)命名空間。在這裏閱讀全部內容:

Managing Directory Security Principals in the .NET Framework 3.5

基本上,你可以定義域範圍內,並可以輕鬆地查找用戶和/或組AD:

// set up domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// find user by name 
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "John Doe"); 

// if we found user - inspect its details 
if(user != null) 
{ 
    string firstName = user.GivenName; 
    string lastName = user.Surname; 
    string email = user.EmailAddress; 
    string phone = user.VoiceTelephoneNumber; 
} 

新S.DS.AM使它真的很容易與玩家和羣體玩AD:

+0

回答marc_s是不完整的。 FindyByIntenty方法至少需要兩個參數。主要上下文對象「ctx」需要作爲第一個參數。 – user55513 2012-08-01 13:31:24

+0

@jdmorris:謝謝,你完全正確 - 更正了我的回答 – 2012-08-01 20:42:30

+1

非常感謝。我整天都在尋找解決方案。這使得它非常簡單易用。 – Ahmad 2013-01-20 10:11:11

1

strPath的是LDAP URL,其上可以在這裏找到更多的信息: http://en.wikipedia.org/wiki/LDAP#LDAP_URLs

結構可能看起來有點怪在第一,但在維基百科中的例子給出了它的一個很好的總結。

相關問題