我正在使用 vb.net,我想检查 Active Directory 中是否存在特定用户。如果是,我想显示特定用户的详细信息。怎么做?
用户登录凭据通过文本框控件传递
My code:
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新手。请帮我做这个吗?
如果您使用的是 .NET 3.5 及更高版本,您应该查看System.DirectoryServices.AccountManagement
(S.DS.AM) 命名空间。在这里阅读所有相关内容:
- 管理 .NET Framework 3.5 中的目录安全主体 http://msdn.microsoft.com/en-us/magazine/cc135979.aspx
- 关于 System.DirectoryServices.AccountManagement 的 MSDN 文档 http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx
基本上,您可以定义域上下文并轻松在 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 中的用户和组的使用变得非常容易!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)