Outlook "Check Names" feature in visual basic.net or C#

- First check out the image attached.
- Suppose I have an asp.net form containing a text field and a button and a list box. I type my windows user name (or part of user name) in the text field and then press button. What i want is that it should check what ever is in text field when i hit button and try to match all email addresses or user names in domain active directory and display their email addresses in list box. Otherwise it should return "no user match".


Screenshot.JPG
LVL 5
Khalid Mehmood AwanAsked:
Who is Participating?
 
DhaestCommented:
I assume that you want to search for users in your active directory.
Some starting points:
Using Active Directory In ASP.NET - Enumerate Active Directory Users
http://www.codeproject.com/KB/aspnet/adsi2.aspx

Searching Active Directory for Users and Groups
http://www.eggheadcafe.com/articles/20051106.asp

Howto: (Almost) Everything In Active Directory via C#
http://www.codeproject.com/KB/system/everythingInAD.aspx
0
 
CodeCruiserCommented:
And for auto complete, check this video

http://www.asp.net/learn/AJAX-videos/video-122.aspx
0
 
Khalid Mehmood AwanAuthor Commented:
One code snippet is attached, the other is here ... This solved my issue.

 Public Shared Function AuthenticateUserPassword(ByVal szUserName As String, ByVal szPassword As String) As Boolean
        Dim result As Boolean = False
        Dim szUserDomain As String = ""
        Dim myLDAPPath As String

        ' Determine what the domain name should be.
        If szUserName.Contains("\") Then
            ' Pull the domain out of the user name.
            szUserDomain = szUserName.Substring(0, szUserName.IndexOf("\"))
            ' Set user name to just user name.
            szUserName = szUserName.Substring(szUserName.IndexOf("\") + 1)
        ElseIf szUserName.Contains("@") Then
            ' User Name is in form of "user@domain"
            ' Pull the domain out of the user name.
            szUserDomain = szUserName.Substring(szUserName.IndexOf("@") + 1)
            ' Set user name to just user name.
            szUserName = szUserName.Substring(0, szUserName.IndexOf("@"))
        Else
            ' A "." refers to the local system.
            szUserDomain = Environment.MachineName
            szUserName = szUserName.Substring(szUserName.IndexOf("\") + 1)
        End If

        ' Determine which entry it will need to be.
        If String.Compare(szUserDomain, Environment.MachineName, True) = 0 Then
            myLDAPPath = "WinNT://" & szUserDomain
        Else
            myLDAPPath = "LDAP://" & szUserDomain
        End If

        Try
            Dim entry As DirectoryEntry = New DirectoryEntry(myLDAPPath, szUserName, szPassword)
            Dim nativeObject As Object = entry.NativeObject
            result = True 'no exception thrown, user must exist
            nativeObject = Nothing  'be sure and clean up these object as this service could be used many times
            entry = Nothing
        Catch ex As Exception
            result = False  'exception thrown - no user with that name/pwd combination
        End Try
        Return result
    End Function
Public Shared Function AD(ByVal strQuery As String, ByVal strReturn As String, ByRef listbox1 As ListBox)
        If strQuery = "" Then
            Exit Function
        End If
        'strQuery = "khalid.mehmood@mobilink.net"

        Dim oroot As DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry("LDAP://mobilink")
        Dim osearcher As DirectoryServices.DirectorySearcher = New DirectoryServices.DirectorySearcher(oroot)
        Dim result As DirectoryServices.SearchResult
        Dim resultAll As DirectoryServices.SearchResultCollection
        'osearcher.Filter = "(&(objectCategory=person)(objectClass=user)(mail=" & strQuery & "))"  ' search filter
        osearcher.Filter = "(&(anr=" & strQuery & ")(mail=*))"  ' search filter
        resultAll = osearcher.FindAll()

        If resultAll.Count > 0 Then
            For i As Integer = 0 To (resultAll.Count - 1)
                listbox1.Items.Add(resultAll(i).Properties("mail")(0).ToString())
            Next
        End If

        oroot = Nothing
        osearcher = Nothing
        result = Nothing
        resultAll = Nothing

        Exit Function
End Function

Open in new window

0
 
Khalid Mehmood AwanAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.