Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code below that should retuurn the fullname of the user logged on. It worked in my other application but for this next application it keeps returning 0. I have added reference to activeds libraby and imported it. Please help.

I call the function like this :
VB
Dim strFullName As String = GetRealNameFromAd(User.Identity.Name)


This is is the function:
VB
Private Function GetRealNameFromAd(ByVal UsernameToFind As String) As String

      Using searcher As New DirectorySearcher(New DirectoryEntry())
          searcher.PageSize = 1000
          searcher.SearchScope = SearchScope.Subtree
          searcher.Filter = "(&(samAccountType=805306368)(sAMAccountName=" & UsernameToFind & "))"
          Using Results As SearchResultCollection = searcher.FindAll

                              If Results Is Nothing OrElse Results.Count <> 0 Then
                  Throw New ApplicationException("Invalid number of results returned - either no users were found or more than one user account was found")
              End If
              Using UserDE As DirectoryEntry = Results(0).GetDirectoryEntry
                  Return CStr(UserDE.Properties("givenName").Value) & " " & CStr(UserDE.Properties("sn").Value)
              End Using
          End Using
      End Using

  End Function
Posted
Updated 23-Apr-14 9:39am
v2

1 solution

What's the error are you getting? I can see one problem with the following If statement. When I changed it to Results.Count = 0, it worked fine for me.

Changed If Results Is Nothing OrElse Results.Count <> 0 Then to If Results Is Nothing OrElse Results.Count = 0 Then, and it worked perfectly fine.
 
Share this answer
 
Comments
Member 10736689 13-May-14 12:59pm    
it says either 0 or more than one results were returned when i use that. and when I check the count is returning 0.i used this same code in a prior application and it worked what seemed to be the problem
Member 10736689 13-May-14 15:00pm    
solved it the problem was the username i was passing in i had to take off the domain.

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