检查用户是否存在于Active Directory(Check if user exists in

2019-09-23 11:45发布

我使用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

当我通过密码。 这是代码是正确的? 我是新来的AD。 请帮助我做到这一点?

Answer 1:

如果你在.NET 3.5及以上,你应该看看System.DirectoryServices.AccountManagement (S.DS.AM)命名空间。 阅读所有关于它在这里:

  • 在.NET Framework 3.5管理目录安全主体
  • 在System.DirectoryServices.AccountManagement MSDN文档

基本上,可以定义一个域上下文和很容易地找到的用户和/或组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使得它可以很容易在公元玩弄用户和组!



文章来源: Check if user exists in Active Directory