Access Active Directory

rsturner8

Member
Joined
Feb 23, 2011
Messages
6
Programming Experience
1-3
I having a program built in VB.Net and would like to take in the users windows name and then compatre that with a security group in AD, if they are in a certain group then redirect them to the next page otherwise block them.


I have tried many ways using many different snippets of code from different forums, but cannot find acode piece of code that works.

I am quite new to programming and have no idea how to connect to the AD.
Any help along with actual snippets of code that work would eb a massive help.
Thanks in advance.
 
First remember to drag DirectoryEntry and DirectorySearcher from the toolbox onto your project and then you need to have

Imports System.DirectoryServices

before your class
then try the code below
replacing
Dim username As String = *******
Dim usergroup As String = *******

for whatever you need to check against and replace DOSOMETHING with the code for what you want to do


VB.NET:
        Dim de As DirectoryEntry = New DirectoryEntry
        Dim ds As DirectorySearcher = New DirectorySearcher
        Dim groupCount As Integer = 0
        Dim groupname As String = ""
        Dim GroupArray() As String
        Dim username As String = *******
        Dim usergroup As String = *******


       ds.Filter = "(&(objectCategory=user)(sammaccountname=" + username.ToString.Trim + "))"
        ds.PropertiesToLoad.Add("memberof")
        Dim result As SearchResult = ds.FindOne()
        If Not (IsNothing(result)) Then
            Try
                groupCount = result.Properties("memberof").Count

            Catch ex As NullReferenceException
                groupCount = 0
            End Try
            For counter = 0 To groupCount - 1
                groupname = CStr(result.Properties("memberof")(counter))
                GroupArray = Split(groupname, ",")
                If Not (IsNothing(GroupArray(0))) Then
                    If GroupArray.ToLower = usergroup.tolower Then DOSOMETHING
                End If
            Next
        End If

I found that elsewhere, but it was a while back, but I cannot remember where..so thanks to whoever originally wrote that
 
I haveny had time to try it yet as I have been put onto another project in work which i will be finishing this week. I will then try this code and let you know, thanks for your help!
 
Sorry didnt see your reply!

Try renaming
toolbox.tbd
toolbox_reset.tbd
toolboxIndex.tbd
toolboxIndex_reset.tbd
to .tdb.old
from "%USERPROFILE%\Local Settings\Application Data\Microsoft\VisualStudio\<version number>
(close VS first)

If it doesnt work rename the files back

(from Controls Toolbox not showing in Visual Studio 2005)
 
Back
Top