Solved

vb.net and ldap group membership query

Posted on 2014-12-03
9
171 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
 
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

Join & Write a Comment

Suggested Solutions

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

762 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