Vb.net 我想使用Active directory检索个人电话号码
当我试图从Active Directory中获取一个人的电话号码时,所有属性都已加载,但除了邮件之外,没有返回任何内容。有人能帮我检索电话号码吗?下面的代码有一些变化?在myrelustpropcollection.propertynames中,只有2是ADSPATH和mail的计数。没有加载其他属性Vb.net 我想使用Active directory检索个人电话号码,vb.net,Vb.net,当我试图从Active Directory中获取一个人的电话号码时,所有属性都已加载,但除了邮件之外,没有返回任何内容。有人能帮我检索电话号码吗?下面的代码有一些变化?在myrelustpropcollection.propertynames中,只有2是ADSPATH和mail的计数。没有加载其他属性 Public Function GetPhoneByName(ByVal Name As String) As String Dim srch As DirectorySearcher
Public Function GetPhoneByName(ByVal Name As String) As String
Dim srch As DirectorySearcher
Dim results As SearchResultCollection = Nothing
Dim phone As Integer
srch = New DirectorySearcher(New DirectoryEntry())
srch.Filter = "(mailnickname=" + Name + ")"
srch.PropertiesToLoad.Add("homephone")
srch.PropertiesToLoad.Add("mail")
srch.PropertiesToLoad.Add("mobile")
srch.PropertiesToLoad.Add("telephoneNumber")
Try
results = srch.FindAll()
Catch ex As Exception
End Try
For Each result In results
Dim myKey As String
Dim myResultPropCollection As ResultPropertyCollection
myResultPropCollection = result.Properties
For Each myKey In myResultPropCollection.PropertyNames
Dim tab1 As String = " "
Dim myCollection As Object
Select Case myKey
Case "mobile" ' Telephone Number
For Each myCollection In myResultPropCollection(myKey)
phone = myCollection.toint
Next myCollection
End Select
Next myKey
Next
Return phone
End Function
这对我来说很好,代码更简洁:
Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices
Imports System.Collections
Public Function GetPhoneByName(Name As String) As String
Dim ctx As New PrincipalContext(ContextType.Domain, "DomainName")
Dim q As New UserPrincipal(ctx)
q.DisplayName = Name
Dim s As PrincipalSearcher = New PrincipalSearcher(q)
Dim ds As DirectorySearcher = s.GetUnderlyingSearcher
ds.PropertiesToLoad.Clear()
ds.PropertiesToLoad.Add("homephone")
ds.PropertiesToLoad.Add("mail")
ds.PropertiesToLoad.Add("mobile")
ds.PropertiesToLoad.Add("telephoneNumber")
For Each dsResult As SearchResult In ds.FindAll()
For Each itm As DictionaryEntry In dsResult.Properties
Select Case itm.Key
Case "mobile"
Return itm.Value(0)
End Select
Next
Next
Return "Not found"
End Function
以下代码将返回active directory中用户可用的所有值:
Imports System.DirectoryServices
Module AdTest
Sub Main()
GetPhoneByName("Persons Display Name")
Console.ReadLine()
End Sub
Public Sub GetPhoneByName(ByVal Name As String)
Dim srch As New DirectorySearcher(New DirectoryEntry())
srch.Filter = "(displayname=" + Name + ")"
For Each result As SearchResult In srch.FindAll()
For Each key As DictionaryEntry In result.Properties
For Each keyVal In result.Properties(key.Key)
Try
Console.WriteLine(key.Key + ": " + keyVal)
Catch ex As Exception
'value of keyVal could not convert to string (probably byte array)
End Try
Next
Next
Next
End Sub
End Module
尼克,谢谢你的密码。你能告诉我为什么我的代码不起作用,无法获取属性的值吗?你是否将“域名”更改为你的域名?或者你的意思是我的代码对你有效而你的不有效?我把域名改成了我的域名。但是这个代码对我不起作用。你确定你要搜索的人有手机号码吗?您可以在outlook中轻松地在他们的联系卡上检查这一点。如果没有,那么你甚至不会在列表中得到一个条目。唯一加载的属性是ADSPATH