Solved

vb.net and ldap group membership query

Posted on 2014-12-03
9
177 Views
Last Modified: 2014-12-04
I have a login method that determines whether or not a user is a member of a certain ad group.  How do i check against multiple groups, i tried the below and it errors with ""

srch.Filter = "(&(objectClass=user)(samaccountname=" & yourUsername & ")(memberOf=group1,OU=SecurityGroups,DC=test,DC=net))" Or srch.Filter =  "(&(objectClass=user)(samaccountname=" & yourUsername & ")(memberOf=cn=Group2,OU=SecurityGroups,DC=test,DC=net))"

Open in new window

0
Comment
Question by:derek7467
  • 5
  • 4
9 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 40478796
try in your text, replace
memberOf=group1
with
memberOf=cn=group1
0
 

Author Comment

by:derek7467
ID: 40478981
i still get Conversion from string "(&(objectClass=user)(samaccountn" to type 'Boolean' is not valid.
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40480272
I guess I need more of your code to check the error then. If not possible, I just assumed it was only part of an IF condition.
If that REALLY is your code, that one line, then it's not correct. Finish with one filter, keep the result. Finish same code, but with second filter, THEN combine the results to get what you need.
0
 

Author Comment

by:derek7467
ID: 40480545
Here is my code:

 If Authenicate(txtUsername.Text, txtPassword.Text) Then
            Dim yourUsername As String = txtUsername.Text
            Dim rootEntry As New DirectoryEntry("LDAP://test.net:389/DC=test,DC=net")
            Dim srch As New DirectorySearcher(rootEntry)

            srch.SearchScope = SearchScope.Subtree
            srch.Filter = "(&(objectClass=user)(samaccountname=" & yourUsername & ")(memberOf=cn=group 1,OU=SecurityGroups,DC=test,DC=net))"

            Dim res As SearchResultCollection = srch.FindAll()

            If res Is Nothing OrElse res.Count <= 0 Then
                MsgBox("You do not have access to this Security Group", , "Authentication Failed")
                txtUsername.Text = ""
                txtPassword.Text = ""
                Label4.Visible = False
                txtUsername.Focus()
            Else
                Label4.Visible = False
                AdvancedTools.Show()
                AdvancedTools.Label1.Text = "Welcome, " & txtUsername.Text
                Me.Hide()
            End If

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 40480563
Yes, as I said, that original code works.

If you want to add another  one, you have to combine the results.


 If Authenicate(txtUsername.Text, txtPassword.Text) Then
            Dim yourUsername As String = txtUsername.Text
            Dim rootEntry As New DirectoryEntry("LDAP://test.net:389/DC=test,DC=net")
            Dim srch As New DirectorySearcher(rootEntry)

            srch.SearchScope = SearchScope.Subtree
            srch.Filter = "(&(objectClass=user)(samaccountname=" & yourUsername & ")(memberOf=cn=group 1,OU=SecurityGroups,DC=test,DC=net))"

            Dim res As SearchResultCollection = srch.FindAll()

			srch.Filter = "(&(objectClass=user)(samaccountname=" & yourUsername & ")(memberOf=cn=group 2,OU=SecurityGroups,DC=test,DC=net))"
			
			Dim res2 As SearchResultCollection = srch.FindAll()			
						
            If res Is Nothing OrElse res.Count <= 0 Then
                MsgBox("You do not have access to this Security Group", , "Authentication Failed")
                txtUsername.Text = ""
                txtPassword.Text = ""
                Label4.Visible = False
                txtUsername.Focus()
            Else
			    If res2 Is Nothing OrElse res2.Count <= 0 Then
					MsgBox("You do not have access to this Security Group", , "Authentication Failed")
					txtUsername.Text = ""
					txtPassword.Text = ""
					Label4.Visible = False
					txtUsername.Focus()
				Else
					Label4.Visible = False
					AdvancedTools.Show()
					AdvancedTools.Label1.Text = "Welcome, " & txtUsername.Text
					Me.Hide()
				End if
            End If

Open in new window

0
 

Author Comment

by:derek7467
ID: 40480663
Ok, so i have access to the first group but not the second and this code does not let me in based off of the first group?

If Authenicate(txtUsername.Text, txtPassword.Text) Then
            Dim yourUsername As String = txtUsername.Text
            Dim rootEntry As New DirectoryEntry("LDAP://test.net:389/DC=test,DC=net")
            Dim srch As New DirectorySearcher(rootEntry)

            srch.SearchScope = SearchScope.Subtree
            srch.Filter = "(&(objectClass=user)(samaccountname=" & yourUsername & ")(memberOf=cn=group1,OU=SecurityGroups,DC=test,DC=net))"

            Dim res As SearchResultCollection = srch.FindAll()

            srch.Filter = "(&(objectClass=user)(samaccountname=" & yourUsername & ")(memberOf=cn=group 2,OU=SecurityGroups,DC=test,DC=net))"
            Dim res2 As SearchResultCollection = srch.FindAll()

            If res Is Nothing OrElse res.Count <= 0 Then
                MsgBox("You do not have access to this Security Group", , "Authentication Failed")
                txtUsername.Text = ""
                txtPassword.Text = ""
                Label4.Visible = False
                txtUsername.Focus()
            Else
                If res2 Is Nothing OrElse res2.Count <= 0 Then
                    MsgBox("You do not have access to this Security Group", , "Authentication Failed")
                    txtUsername.Text = ""
                    txtPassword.Text = ""
                    Label4.Visible = False
                    txtUsername.Focus()
                Else
                    Label4.Visible = False
                    AdvancedTools.Show()
                    AdvancedTools.Label1.Text = "Welcome, " & txtUsername.Text
                    Me.Hide()
                End If
            End If
        Else
            MsgBox("Incorrect Credentials", , "Login Failed")
            txtUsername.Text = ""
            txtPassword.Text = ""
            Label4.Visible = False
            txtUsername.Focus()
        End If

Open in new window

0
 

Author Comment

by:derek7467
ID: 40480679
Got, the else statements were jacked:  The below are correct:

If res Is Nothing OrElse res.Count <= 0 Then
                MsgBox("You do not have access to this Security Group", , "Authentication Failed")
                txtUsername.Text = ""
                txtPassword.Text = ""
                Label4.Visible = False
                txtUsername.Focus()
            ElseIf res2 Is Nothing OrElse res.Count <= 0 Then
                MsgBox("You do not have access to this Security Group", , "Authentication Failed")
                txtUsername.Text = ""
                txtPassword.Text = ""
                Label4.Visible = False
                txtUsername.Focus()
            Else
                Label4.Visible = False
                AdvancedTools.Show()
                AdvancedTools.Label1.Text = "Welcome, " & txtUsername.Text
                Me.Hide()
            End If
        Else
            MsgBox("Incorrect Credentials", , "Login Failed")
            txtUsername.Text = ""
            txtPassword.Text = ""
            Label4.Visible = False
            txtUsername.Focus()
        End If

Open in new window

0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40480927
Yes, sorry I didn't understand your code correctly. My code was just to lead you in the right direction.

But your final code, I think:

ElseIf res2 Is Nothing OrElse res.Count <= 0 Then

Open in new window


should be

ElseIf res2 Is Nothing OrElse res2.Count <= 0 Then

Open in new window

0
 

Author Comment

by:derek7467
ID: 40480956
yep, missed that second res2, thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now