1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| Public Shared Function GetUserInfobyDomain(ByVal inSAM As String, ByVal inType As String, ByVal Domain As String) As String
Using HostingEnvironment.Impersonate()
' Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext
' Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
Try
Dim LDAP As DirectoryEntry
LDAP = New DirectoryEntry("LDAP://toncontroleurdedomain:389/OU=Users,OU=Identities,DC=MYDOMAIN,DC=COMP,DC=CORP", "MYDOMAIN.COMP.CORP\MyServiceAccount", "MyPassword")
Dim SamAccount As String = Right(inSAM, Len(inSAM) - InStr(inSAM, "\"))
LDAP.AuthenticationType = AuthenticationTypes.ServerBind
Dim mySearcher As New DirectorySearcher(LDAP)
Dim mySearchResultColl As SearchResultCollection
Dim mySearchResult As SearchResult
Dim myResultPropColl As ResultPropertyCollection
Dim myResultPropValueColl As ResultPropertyValueCollection
'Build LDAP query
mySearcher.Filter = ("(&(objectClass=user)(samaccountname=" & SamAccount & "))")
mySearchResultColl = mySearcher.FindAll()
'I expect only one user from search result
Select Case mySearchResultColl.Count
Case 0
Return "Null"
Exit Function
Case Is > 1
Return "Null"
Exit Function
End Select
'Get the search result from the collection
mySearchResult = mySearchResultColl.Item(0)
'Get the Properites, they contain the usefull info
myResultPropColl = mySearchResult.Properties
'displayname, mail
'Retrieve from the properties collection the display name and email of the user
myResultPropValueColl = myResultPropColl.Item(inType)
Return CStr(myResultPropValueColl.Item(0))
Catch ex As System.Exception
Return "" 'ex.ToString
'do some error return here.
End Try
End Using
End Function |
Partager