?
Solved

vb.net and ldap group membership query

Posted on 2014-12-03
9
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 36

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 36

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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 36

Accepted Solution

by:
Kimputer earned 1500 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 36

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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Suggested Courses

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