本文介绍了在AD中使用登录名进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好, 我想在此请求中根据他的登录名Hello,I would like to search someone based on his logon name sAMAccountname搜索某人:Dim objUser = GetObject("LDAP://194.7.23.169/cn=" + CUID + ",OU=Users,OU=Operations,OU=Orange,DC=qconsulting,DC=local" 我尝试了什么: 我试图用sAMAccountname替换cn,sn ,... What I have tried:I tried to replace cn by sAMAccountname, sn, ...推荐答案 Function GetDefaultNamingContext() As String Dim rootDse As New DirectoryEntry("LDAP://rootDSE") Dim domainDn = rootDse.Properties("DefaultNamingContext").Value.ToString Return domainDnEnd Function 现在,需要一种方法来搜索AD用户对象的samAccountName。这比单行代码要复杂得多: Now, need a method to search for an AD User object by its samAccountName. That's quite a bit more complicated than a single line of code:Function GetAdObjectForSamAccount(ByVal samAccountName As String) ' Validate we have something to search for. If [String].IsNullOrEmpty(samAccountName) Then Throw New ArgumentNullException(samAccountName) End If ' Get the directory context to search. Dim context As String = GetDefaultNamingContext() ' Setup a search for a samAccountName. ' First, point to the directory to search. Using directory As New DirectoryEntry( 为什么这样做?因为您可以从其他人那里调用此方法,所有这些方法都可能以某种方式使用User对象,但方式不同。这些方法中的每一个都可以使用此方法仅基于samAccountName为它们获取User对象。 例如,返回特定samAccountName的DisplayName的方法或者你称之为登录名。 Why do this? Because you can call this method from others that all might work with a User object somehow, but in different ways. Each of those methods can use this method to grab a User object for them just based on the samAccountName.For example, a method to return the DisplayName for a particular samAccountName, or what you're calling the "logon name".Function GetDisplayNameForSamAccount(ByVal samAccountName As String) As String ' Validate we have something to search for. If [String].IsNullOrEmpty(samAccountName) Then Throw New ArgumentNullException(samAccountName) End If Dim displayName As String = String.Empty Dim adObject As DirectoryEntry = GetAdObjectForSamAccount(samAccountName) If adObject IsNot Nothing Then displayName = adObject.Properties("DisplayName")(0).ToString End If Return displayNameEnd Function 拨打此电话并获取您正在寻找的全名非常简单: And to call this and get the full name you're looking for is really easy:Dim name As String = GetDisplayNameForSamAccount("BCNF0167")Console.WriteLine( 这篇关于在AD中使用登录名进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 18:05