3

我正在使用 vb.net,我想检查 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 = "ss@123"
    'Dim searcher As New DirectorySearcher
    'searcher.SearchRoot = dirEntry
    Return dirEntry
End Function

我在哪里传递密码。这段代码是否正确?我是新来的广告。请帮我这样做?

4

1 回答 1

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 中与用户和组一起玩变得非常容易!

于 2012-10-04T07:40:15.790 回答