Click here to Skip to main content
15,122,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am looking to implement Automatic Active Directory Logon to one of our intranet Applications.

Groups and information needs to extracted from Active Directory

The app was coded in CodeCharge Studio 4.3 so I am unable to open of export the project to Visual Studio (Witch in itself is a problem for me)

It only runs on Chrome and Firefox

My Setup:
Win 2008 R2 Enterprise

Tried the example:

Imports System.DirectoryServices

Public Class Test1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        REM Handles Me.Load
        Dim strUser As String
        Dim binFlag As Boolean

        REM This is equivalent to adsRoot=GetObject("LDAP://OU=ADSI,DC=ds,DC=microsoft,DC=com")
        REM and opens a connection to the root of the directory that you
        REM would want to search. Replace the string with your directory service root.
        Dim adsRoot As New DirectoryEntry("LDAP://OU=ADSI,DC=...,DC=...,DC=...")

        REM This sets up the filter to be used in searching for the user in AD.
        Dim adsSearch As DirectorySearcher = New DirectorySearcher(adsRoot)

        REM Grab the User ID of the person pulling the page, or the sAMAccountName
        strUser = Page.User.Identity.Name

        REM Strip off domain name (we already know it, although in multi-domain
        REM environments you might find that useful.
        strUser = Mid(strUser, InStr(1, strUser, "\") + 1)

        REM Search Active Directory For the user via
        REM System.DirectoryServices.DirectorySearcher
            REM We'll load the filter with the items we want to fetch,
            REM similar to a SQL statement.
            REM The first is what we are looking for, the sAMAccountName.
            REM We will also need the group membership of the user once
            REM we have found the user.
            REM We are likely to also need the common name, although
            REM it's not needed for this example.
            REM We don't need the .FullName property for this example,
            REM but you might, so I show it here.
            REM build the search filter (looking for the user with a login
            REM name that matches who connected to the page.
            adsSearch.Filter = "sAMAccountName=" & strUser

            REM Get some variables ready to receive the results
            Dim oResult As SearchResult
            Dim RetArray As New Hashtable()
            Dim adsGrpcn As String
            binFlag = False

            REM Now get the results (just one), what you get back is 
            REM an object that points to the found user
            oResult = adsSearch.FindOne
            REM You can now loop through the list of groups
            For Each adsGrpcn In oResult.GetDirectoryEntry().Properties("memberof").Value
                REM You'll want to splice this string a bit to match a specific group
                REM Then test to see if it matches your application group. Make sure to
                REM use TRIM() to avoid embedded spaces in the common name of the group.
                If adsGrpcn = "MyGroup" Then binFlag = True
        Catch ex As Exception
            Response.Write("I got the following error while trying to authenticate you: " & ex.Message)
        End Try
        If binFlag Then
            Response.Write("You are authorized!")
            Response.Write("You are not authorized!")
        End If
    End Sub

End Class

Error I got:
I got the following error while trying to authenticate you: There is no such object on the server.

I am not an .Net expert and need some help.

Had a look at most of the articles and none of them helps me exactly.

Thanks for you help and time
Updated 4-Apr-13 23:04pm
[no name] 4-Apr-13 9:51am
I you want some help, you would first have to explain what "Could not get it to work" means.
Canafunk 4-Apr-13 10:14am
Added some more details to the Question, does this help?

1 solution


This code:
strUser = Page.User.Identity.Name

Returns the domain and username of current user.

When you make a ldap search like this:
adsSearch.Filter = "sAMAccountName=" & strUser

You need to remove the domain name from strUser, because you are already querying the domain.

Hope it helps.

Good luck.
Canafunk 5-Apr-13 3:01am
strUser = Mid(strUser, InStr(1, strUser, "\") + 1)

This should take care of the domain name? Or is that not what you mean?
José Amílcar Casimiro 5-Apr-13 10:41am
You should not use the domain name.

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