Solved

vb.net and ldap group membership query

Posted on 2014-12-03
9
186 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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