Link to home
Start Free TrialLog in
Avatar of Khalid Mehmood Awan
Khalid Mehmood AwanFlag for Pakistan

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Khalid Mehmood Awan

ASKER

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

Thanks