2012-10-04 113 views
3

我正在使用vb.net,我想檢查某個特定用戶是否存在於Active Directory中。如果是這樣,我想顯示特定用戶的詳細信息。怎麼做?檢查用戶是否存在於Active Directory中

用戶的登錄憑據是通過文本框控件傳遞

我的代碼:

Dim de As DirectoryEntry = GetDirectoryEntry() 
Dim ds As DirectorySearcher = New DirectorySearcher(de) 
    ds.Filter = "(&(objectClass=txt1.text))" 

    ' Use the FindAll method to return objects to SearchResultCollection. 
    results = ds.FindAll() 

Public Shared Function GetDirectoryEntry() As DirectoryEntry 
    Dim dirEntry As DirectoryEntry = New DirectoryEntry() 
    dirEntry.Path = "LDAP://ss.in:389/CN=Schema,CN=Configuration,DC=ss,DC=in" 
    dirEntry.Username = "ss.in\ssldap" 
    dirEntry.Password = "[email protected]" 
    'Dim searcher As New DirectorySearcher 
    'searcher.SearchRoot = dirEntry 
    Return dirEntry 
End Function 

如果我通過了密碼。這段代碼是正確的嗎?我是AD新手。請幫我做這個?

回答

5

如果您使用的是.NET 3.5及更高版本,則應檢查System.DirectoryServices.AccountManagement(S.DS.AM)命名空間。在這裏閱讀全部內容:

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

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

// find a user 
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); 

if(user != null) 
{ 
    // your user exists - do something here....  
} 
else 
{ 
    // your user in question does *not* exist - do something else.... 
} 

或者在VB.NET中:

' set up domain context 
Dim ctx As New PrincipalContext(ContextType.Domain) 

' find a user 
Dim user As UserPrincipal = UserPrincipal.FindByIdentity(ctx, "SomeUserName") 

If user IsNot Nothing Then 
    ' your user exists - do something here....    
Else 
    ' your user in question does *not* exist - do something else.... 
End If 

新的S.DS.AM讓您可以輕鬆地與AD中的用戶和羣組玩耍!

+0

好的非常感謝你。我會嘗試並回復你後者 – vps

+0

是我在aspx.vb中包含任何名稱空間頁 – vps

+0

我應該在哪裏傳遞域參數@marc_s – vps

相關問題