Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET active directory , +
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
IIS7

 
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
        Try
            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.
            adsSearch.PropertiesToLoad.Add("sAMAccountName")
            REM We will also need the group membership of the user once
            REM we have found the user.
            adsSearch.PropertiesToLoad.Add("memberof")
            REM We are likely to also need the common name, although
            REM it's not needed for this example.
            adsSearch.PropertiesToLoad.Add("cn")
            REM We don't need the .FullName property for this example,
            REM but you might, so I show it here.
            adsSearch.PropertiesToLoad.Add("FullName")
            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.
                Response.Write(adsGrpcn)
                If adsGrpcn = "MyGroup" Then binFlag = True
            Next
        Catch ex As Exception
            Response.Write("I got the following error while trying to authenticate you: " & ex.Message)
            Response.End()
        End Try
        If binFlag Then
            Response.Write("You are authorized!")
        Else
            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
Posted 4-Apr-13 4:48am
Edited 4-Apr-13 23:04pm
v4
Comments
ThePhantomUpvoter at 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 at 4-Apr-13 10:14am
   
Added some more details to the Question, does this help?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
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.
  Permalink  
Comments
Canafunk at 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?
   
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)

  Print Answers RSS
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100