We help IT Professionals succeed at work.

Need help to read Active directory users into an ASP.net application.

saruman101
saruman101 asked
on
243 Views
Last Modified: 2012-05-05
Hey whats up experts, I just want to know how can I read active directory users,name,etc
and then put it in a table or grid for my asp.net application.

I have no idea how to do this...
is it similar to reading a database and then filling it in a dataset
or you have to do it some other way.

Please break it down to me sinces this is my first time doing this.

Thanks in advance guys.
Comment
Watch Question

Author

Commented:
Also is it possible to read off specific AD folders ? ?

Author

Commented:
also im using asp.net with VB.net

Author

Commented:
Ok guys this is the verdict. I have 2 textboxes.(username,password) then I can compare that to AD to see if it is true.
if it is then I would like to be able to retrieve the Group he/she is in . That is why I said if it is possible to reader specific OU withins Active directory.

This is my first time trying this out.

Author

Commented:
so any other suggestion besides these links?...I tried some of the samples and I don't know what I'm doing wrong
but when I try to import some of the imports to my class project they are not found...

Am I doing something wrong?

Also is there an easier way to do this?

Author

Commented:
Is there any one out there willing to help?

Commented:
http://www.codeproject.com/vb/net/LDAP_Using_VBnet.asp seems to work with a little tweaking

Author

Commented:
Ok I will try to give it a shot and see I will let you know more soon

Author

Commented:
Ok I know its been a long time I haven't responded, I ve been pretty busy. Well Now I had the chance to test it out , it seems like the code is close to what I want, but so far I get this error. I don't know if I'm doing it correctly or not...but I used the example you gave me from the link and I decided to try it in vb.net, once it works I can tweak it a bit to work for my asp.net page.

The error message I get is this
"A referral was returned from the server"

That is what the catch exception gives me

Here is the code I use , pretty much the same as the linked page
////
Dim _ldapServerName As String = ldapServerName

        Dim sServerName As String = "mail"

        Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & _
              "/ou=User Accounts,dc=jewelamerica,dc=com")

        Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
        Dim oResults As SearchResultCollection
        Dim oResult As SearchResult
        Dim RetArray As New Hashtable

        Try

            oSearcher.PropertiesToLoad.Add("uid")
            oSearcher.PropertiesToLoad.Add("givenname")
            oSearcher.PropertiesToLoad.Add("cn")
            oResults = oSearcher.FindAll

            For Each oResult In oResults

                If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then
                    RetArray.Add(oResult.GetDirectoryEntry().Properties("uid").Value, _
                      oResult.GetDirectoryEntry().Properties("cn").Value)
                End If

            Next

        Catch e As Exception

            MsgBox("Error is " & e.Message)
            Return RetArray

        End Try

        Return RetArray
///

Author

Commented:
so um any suggestions ?

Author

Commented:
Ok well I just wanted to update the status. Well I figure out that I had the incorrect value for the servername so once I correct this I have my catch block catches another error which it say
"Key Cannot Be Null "

Is there anyway around this. ?
Based on the code I just want to be able to read off from AD and so far I guess I'm getting close to making this happen, but so far I m stuck with this error now.

If anyone knows how to deal with this please post.

Commented:
try 1 or 2 small changes.  You can delete "Dim _ldapServerName As String = ldapServerName"
Then change your DirectoryEntry to use a string.  You will need to change "LDAP://toplevel.actdir.whatever.com" to the correct 1 for your own network, eg. "LDAP://your1st.your2nd.etc.etc".

I think you may also need to make sure that your website in IIS is set to use Windows Authentication.  You may also need to set impersonate on in your web.config file, ie.
<identity impersonate="true" />

    Public Function GetAllUsers() As Hashtable

        'To retrieve list of all LDAP users

        'This function returns HashTable
        Dim sServerName As String = "mail"

        Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://toplevel.actdir.whatever.com")

        Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
        Dim oResults As SearchResultCollection
        Dim oResult As SearchResult
        Dim RetArray As New Hashtable

        Try

            oSearcher.PropertiesToLoad.Add("uid")
            oSearcher.PropertiesToLoad.Add("givenname")
            oSearcher.PropertiesToLoad.Add("cn")
            oResults = oSearcher.FindAll

            For Each oResult In oResults

                If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then
                    TextBox1.Text += (oResult.GetDirectoryEntry().Properties("adspath").Value + " and: " + _
                      oResult.GetDirectoryEntry().Properties("cn").Value)
                    '                    RetArray.Add(oResult.GetDirectoryEntry().Properties("adspath").Value, _
                    '                     oResult.GetDirectoryEntry().Properties("cn").Value)
                End If

            Next

        Catch e As Exception

            MsgBox("Error is " & e.Message)
            Return RetArray

        End Try

        Return RetArray

    End Function

Author

Commented:
ok sorry I haven't responded in a long time, I have been bombarded with other projects lol.
anyhow I jump back on this one for a bit and I still get this error.
("Key can not be null")

That is caught by my exception.

Also I modified this part of the code
For Each oResult In oResults

                If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then
                    TextBox1.Text += (oResult.GetDirectoryEntry().Properties("adspath").Value + " and: " + _
                      oResult.GetDirectoryEntry().Properties("cn").Value)
                    '                    RetArray.Add(oResult.GetDirectoryEntry().Properties("adspath").Value, _
                    '                     oResult.GetDirectoryEntry().Properties("cn").Value)
                End If

            Next

I comment the if loops and I played around with a list box...well When I used the list box I
coded in (.oResult.GetDirectoryEntry.Name) Within the list box and to my surprise I was able to read off
the directory through the list box.

I don't understand why I can read it into a list box, but when I attempt to do the if loop again I still get
the error message of ("Key can not be null")

..Remember I still doing this in VB.NET and I will transport the code to an ASP.net page once I get it working on the VB part .

Also last but not least once I get my Active directory reading off the users from a particular OU ..Is there a way to query Active directory ...kinda like SQL query in order to see if a particular user exist ?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Ok I will give it a shot stumpy1 and I will keep you updated
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.