Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I would like to search someone based on his logon name
sAMAccountname
on this request :
Dim objUser = GetObject("LDAP://194.7.23.169/cn=" + CUID + ",OU=Users,OU=Operations,OU=Orange,DC=qconsulting,DC=local"


What I have tried:

I tried to replace cn by sAMAccountname, sn, ...
Posted
Updated 26-Dec-18 4:36am
Comments
Dave Kreskowiak 26-Dec-18 9:45am    
And the problem would be...?
Jérôme Woody Maisetti 26-Dec-18 9:49am    
It's not working.
For example, My logon name is BCNF0167.

If I search with : GetObject("LDAP://194.7.23.169/cn=" + CUID + ",OU=Users,OU=Operations,OU=Orange,DC=qconsulting,DC=local" , I type my name, and he gives me my logon (BCNF0167).

But what I want is, put BCNF0167, and he gives me my name.
Dave Kreskowiak 26-Dec-18 9:51am    
So is it not finding the AD object entirely or you don't know who to retrieve the DisplayName?

"Not working" is not a problem description. You've MUST use the debugger to narrow down the problem, otherwise you're just guessing and chasing ghosts.
Jérôme Woody Maisetti 26-Dec-18 9:53am    
So, basically, I don't know which parameter to put instead of "CN" to match with the logon name.

1 solution

OK, GetObject isn't really appropriate for this. This requires quite a bit more code to work properly.

First, your code is hard-coding the directory path to "search", as your code doesn't really do any searching at all. It also hard-codes the IP to use. That's bad because if that server is down or the IP changes, your search is going to fail. Also, if the directory path changes at all, your code has to be changed to accommodate the changes in the directory.

That is easily solved with a simple method:
VB.NET
Function GetDefaultNamingContext() As String
    Dim rootDse As New DirectoryEntry("LDAP://rootDSE")

    Dim domainDn = rootDse.Properties("DefaultNamingContext").Value.ToString

    Return domainDn
End Function

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:
VB.NET
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($"LDAP://{context}")
        ' Create a Searcher to do the work for us.
        Using searcher As New DirectorySearcher(directory, $"samAccountName={samAccountName}")
            ' Search the entire subtree of the directory because
            ' Users can really be stored anywhere in a tree.
            searcher.SearchScope = SearchScope.Subtree

            ' This will store the result of the search
            Dim result As SearchResult

            ' This is going to be the DirectoryEntry object we
            ' return, if any.
            Dim returnObject As DirectoryEntry = Nothing

            ' Search the diroectory for the object we want.
            result = searcher.FindOne

            ' Check if an object result was found.
            If result IsNot Nothing Then
                ' Grab the DirectoryEntry object for the found object.
                returnObject = result.GetDirectoryEntry
            End If

            ' Return whatever we found back to the caller.
            Return returnObject
        End Using
    End Using
End Function

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".
VB.NET
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 displayName
End Function

And to call this and get the full name you're looking for is really easy:
VB.NET
Dim name As String = GetDisplayNameForSamAccount("BCNF0167")

Console.WriteLine($"DisplayName = {name}")
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900