Difficulty Quering Active Directory on MemberOf

I 'm working on adding security to an internal web app.  I'm trying to program it to check the groups the logged in user belongs to.  If they belong to the required group(s) then the program will give the user access.

I'm making it so members of TELMIS will have admin access.  When I try to login it queries for my groups but it returns everything except TELMIS.  I figured it may be because TELMIS is set as my primary group in AD.  So I changed my primary group to another group.  Then, TELMIS is returned but the one I set to primary isn't.  I don't understand why it won't return the primary group in the list of groups.  Can someone explain and help me modify my code to get this to work.

        Using de As New DirectoryEntry("LDAP://" + MyDomain)

            Using deSearch As New DirectorySearcher()
                deSearch.PageSize = 8000
                deSearch.SearchRoot = de
                deSearch.Asynchronous = True
                deSearch.Filter = "(&(objectClass=user)(userprincipalname=" + Username() + "))"

                Dim result As SearchResult = deSearch.FindOne()
                If result Is Nothing Then
                    Return False
                End If

                If result.Properties.Contains("memberof") Then
                    For Each item In result.Properties("memberof") ' collection

                        Dim value As String = item.ToString()
                        If value.Contains("CN=TELMIS") Then
                            Return True
                        End If

                End If

            End Using

        End Using

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Scott

Basically the primary group doesnt appear in the memberof collection it is accessed sperately:

Have a look at this, http://support.microsoft.com/kb/321360

Am looking just now at how you can implement this



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ScottMacMasterAuthor Commented:
Well, I can understand not storing the primary group in the list of groups to save space.  However, why force end users to query 2 different things to find out if if a user if a member of a group.  AD could at least return the primary group amoung the list of members.  Well, I guess I just need to add code to get the primary group.  Any chance you know of a reference that shows how to do that in VB.NET not ADSI?

ScottMacMasterAuthor Commented:
Oh, I can just use the global variable 'User'.  I've always used it to get the name of the logged in user.  I can use User.IsInRole("TELMIS") to check the users membership.  And it works whether the group is primary or not.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.