检查用户是否存在于Active Directory中 [英] Check if user exists in Active Directory
问题描述
我正在使用vb.net,我想检查Active Directory中是否存在特定用户.如果是这样,我想显示特定用户的详细信息.怎么做?
I am using vb.net and I want to check whether a particular user exists in Active Directory. If it does, I want to display the particular user's details. How to do it?
用户登录凭据通过文本框控件传递
User login credentials are passed via textbox control
我的代码:
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的新手.请帮我这样做吗?
Where I pass the password. Is this code is correct? I am new to AD. Pls help me to do this?
推荐答案
如果您使用的是.NET 3.5及更高版本,则应签出System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间.在这里阅读所有内容:
If you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement
(S.DS.AM) namespace. Read all about it here:
- Managing Directory Security Principals in the .NET Framework 3.5
- MSDN docs on System.DirectoryServices.AccountManagement
基本上,您可以定义域上下文并轻松找到AD中的用户和/或组:
Basically, you can define a domain context and easily find users and/or groups in 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中的用户和组玩起来非常容易!
The new S.DS.AM makes it really easy to play around with users and groups in AD!
这篇关于检查用户是否存在于Active Directory中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!