Я пытаюсь получить полное имя пользователя с учетом их идентификатора пользователя «domain \ usarname». Попробовал несколько разных примеров, но никто, похоже, не работает. Я использую Visual Basic .Net 2010.
Я изначально нашел следующий код в VBS и портирован в VBA и работал красиво. Если я пытаюсь использовать один и тот же код в VB.NET 2010, я получаю несколько ошибок и путь LDAP не найден, даже если я ввещу его вручную.
Function FindUser()
On Error GoTo Err
Dim objRoot As Variant
Dim LDAPdomainName As String
Dim UserName As String
Dim UserDomain As String
Dim cn As Variant
Dim cmd As Variant
Dim rs As Variant
UserName = VBA.Environ("UserName") ' Gets Current User
UserDomain = VBA.Environ("UserDomain") 'Gets Current User's Domain
Set objRoot = GetObject("LDAP://RootDSE")
Domain= objRoot.Get("defaultNamingContext")
Set cn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
Set rs = CreateObject("ADODB.Recordset")
cn.Open "Provider=ADsDSOObject;"
cmd.activeconnection = cn
'cmd.commandtext = "SELECT ADsPath FROM 'LDAP://" & Domain & "' WHERE sAMAccountName = '" & UserName & "'"
'To see all attributes names available, connect with Active Directory Explorer and add to Select.
cmd.commandtext = "SELECT cn, mail FROM 'LDAP://" & Domain & "' WHERE sAMAccountName = '" & UserName & "'"
Set rs = cmd.Execute
Do Until rs.EOF
Debug.Print rs("cn") & " E-mail: " & rs("mail")
rs.MoveNext
Loop
Exit_Err:
If Not rs Is Nothing Then rs.Close
If Not cn Is Nothing Then cn.Close
Set rs = Nothing
Set cmd = Nothing
Set cn = Nothing
Exit Function
Err:
If Err <> 0 Then
MsgBox "Error connecting to Active Directory Database: " & Err.Description
Else
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
MsgBox rs(0)
Else
MsgBox "Not Found"
End If
End If
Resume Exit_Err
End Function
Если вы используете .NET 3.5 и выше, вы должны проверить пространство имен System.DirectoryServices.AccountManagement
(S.DS.AM). Прочтите все об этом здесь:
В принципе, вы можете определить контекст домена и легко найти пользователей и/или группы в AD:
' set up domain context
Dim ctx As New PrincipalContext(ContextType.Domain)
' find a user
Dim user As UserPrincipal = UserPrincipal.FindByIdentity(ctx, "domain\username")
' do something here....
If user IsNot Nothing Then
End If
' find the group in question
Dim group As GroupPrincipal = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere")
' if found....
If group IsNot Nothing Then
' iterate over members
For Each p As Principal In group.GetMembers()
' do whatever you need to do to those members
Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName)
Next
End If
Новый S.DS.AM позволяет очень легко играть с пользователями и группами в AD!
У меня есть 2 функции, которые помогли мне сделать это с .Net 2.0 до .Net 4.0 после быстрого просмотра MSDN этот должен работать во всех версиях .Net runtimes.
2 функции:
'Determines your domain name
Private Function DomainName() As String
Dim objRootDSE As New System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
DomainName = objRootDSE.Properties("defaultNamingContext")(0)
End Function
'Will output user first name and last name.
Public Sub ReturnUserName(ByVal UserAccountName As String)
' add a reference to System.DirectoryServices.dll
Dim srch As System.DirectoryServices.DirectorySearcher
Dim result As System.DirectoryServices.SearchResult
Dim de, dir As System.DirectoryServices.DirectoryEntry
de = New System.DirectoryServices.DirectoryEntry("LDAP://" & DomainName())
srch = New System.DirectoryServices.DirectorySearcher(de)
srch.SearchScope = SearchScope.Subtree
srch.PropertiesToLoad.Add("givenName")
srch.PropertiesToLoad.Add("sn")
'Other field examples:
'srch.PropertiesToLoad.Add("distinguishedName")
'srch.PropertiesToLoad.Add("uid")
' users require both "user" and "person" filters
' and we also add the sAMAccountName to get the user passed.
' If you want to return all users in the domain remove the (sAMAccountName=" & UserAccountName & ")
' from the filter below.
srch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" & UserAccountName & "))"
For Each result In srch.FindAll()
dir = result.GetDirectoryEntry
' Properties are case sensitive!
Debug.WriteLine(dir.Properties("givenname").Value & " " & dir.Properties("cn").Value)
Next
End Sub
Примером этого может служить:
Public Sub TestUserCall()
'Returns the current logged in user.
Call ReturnUserName(System.Security.Principal.WindowsIdentity.GetCurrent.Name)
End Sub
Этот пример вызова будет работать в версиях версий от 2.0 до 4.0, и снова должен работать во всех версиях, выпущенных до сих пор.
Связанные страницы MSDN:
http://msdn.microsoft.com/ru -us/библиотека/94se97ay (v = VS.80) .aspx
http://msdn.microsoft. ком/EN-US/библиотека/system.directoryservices.searchresult (v = VS.80) .aspx
Вы можете использовать System.DirectoryServices пространство имен для выполнения такого рода задач ( DirectoryServices - управляемая оболочка для LDAP).
Try
' Bind to the users container.
Dim entry As New
DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com")
' Create a DirectorySearcher object.
Dim mySearcher As New DirectorySearcher(entry)
' Create a SearchResultCollection object to hold a collection
' of SearchResults returned by the FindAll method.
Dim result As SearchResultCollection = mySearcher.FindAll()
' Create an object to hold a single result from the
' result collection.
Dim resEnt1 As SearchResult
' Get search results. For more information,
' see Getting Search Results.
'
' This sample uses Try...Catch to catch errors.
' Create an Exception object. For more information,
' see System.Exception.
Catch Exception1 As System.Runtime.InteropServices.COMException
Console.WriteLine(Exception1.Message)
Catch Exception2 As InvalidOperationException
Console.WriteLine(Exception2.Message)
Catch Exception3 As NotSupportedException
Console.WriteLine(Exception3.Message)
End Try
You can use a search string such as "(&(objectCategory=user)(objectClass=person)(sAMAccountName=" + userId + "))"
to search for a user (userID would need to be replaced with the User's ID).
Чтобы связать все это вместе, вы можете изменить фрагмент ниже, чтобы отменить всех свойств для пользователя. Затем вы можете настроить его, чтобы сосредоточиться только на имени пользователя.
Dim results As SearchResultCollection = Nothing
Try
' Bind to the users container.
Dim path As String = "LDAP://CN=users,DC=fabrikam,DC=com"
path = "LDAP://CN=Users,DC=strohmadom,DC=nttest,DC=microsoft,DC=com"
Dim entry As New DirectoryEntry(path)
' Create a DirectorySearcher object.
Dim mySearcher As New DirectorySearcher(entry)
' Set a filter for users with the name test.
mySearcher.Filter = "(&(objectClass=user)(anr=test*))"
' Use the FindAll method to return objects to a SearchResultCollection.
results = mySearcher.FindAll()
' Iterate through each SearchResult in the SearchResultCollection.
Dim searchResult As SearchResult
For Each searchResult In results
' Display the path of the object found.
Console.WriteLine("Search properties for {0}", _
searchResult.Path)
' Iterate through each property name in each SearchResult.
Dim propertyKey As String
For Each propertyKey In searchResult.Properties.PropertyNames
' Retrieve the value assigned to that property name
' in the ResultPropertyValueCollection.
Dim valueCollection As ResultPropertyValueCollection = searchResult.Properties(propertyKey)
' Iterate through values for each property name in each SearchResult.
Dim propertyValue As Object
For Each propertyValue In valueCollection
' Handle results. Be aware that the following
' WriteLine() only returns readable results for
' properties that are strings.
Console.WriteLine("{0}:{1}", _
propertyKey, _
propertyValue.ToString())
Next propertyValue
Next propertyKey
Next searchResult
Finally
' To prevent memory leaks, always call
' SearchResultCollection.Dispose() manually.
If Not results Is Nothing Then
results.Dispose()
results = Nothing
End If
End Try