Solved

vb.net and ldap group membership query

Posted on 2014-12-03
9
191 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 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
Office 365 Training for Admins - 7 Day Trial

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.

 

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

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

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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 …
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.

734 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