Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to Active Directory with user credentials

Status
Not open for further replies.

fatcodeguy

Programmer
Feb 25, 2002
281
CA
Hi,

I'm writing an app (or webservice) that will use Active Directory to authenticate a user. When a user goes to web app, i want it to get his credentials, connect to Active Directory as that user, and get some attributes.

Any idea how to do this? I know how to connect to AD and get the attributes. It's connecting with the logged on user's credentials i'm having trouble with.

Code:
Dim rootDSE As New DirectoryEntry("LDAP://rootDSE")
        'get default naming context doman
        Dim domain As String = rootDSE.Properties("DefaultNamingContext")(0)
        'connect to default naming context
        Dim directoryEntry As New DirectoryEntry("LDAP://" & domain)
        lblUserDetails.Text &= "LDAP User: " & directoryEntry.Username & "<br/>"

        'Bind to the native object to force authentication to happen
        Dim native As Object = directoryEntry.NativeObject

        'Connected to AD.
        'Seach AD
        Dim searcher As New DirectorySearcher(directoryEntry)
        Dim results As SearchResultCollection
        Dim result As System.DirectoryServices.SearchResult
        Dim propertyName As String
        Dim propertyValue As String

        'set searcher settings
        searcher.PageSize = 500
        searcher.CacheResults = False
        searcher.SearchScope = SearchScope.Subtree
        searcher.SizeLimit = 5000

        'set filter
        searcher.Filter = "(&(objectcategory=User)(sAMAccountName=" & sAMAccountName & "))"

        'get results
        results = searcher.FindAll
        lblUserDetails.Text &= "<br/><br/>"
        For Each result In results
            lblUserDetails.Text &= "USER PROPERTIES<br/>"
            lblUserDetails.Text &= "-------------------------------------------<br/>"
            For Each propertyName In result.Properties.PropertyNames
                propertyValue = result.Properties(propertyName).Item(0).ToString
                lblUserDetails.Text &= propertyName & " = " & propertyValue & "<br/>"
            Next
            lblUserDetails.Text &= "<br/><br/>"
        Next
        directoryEntry.Close()

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top