Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to determine if a UserID is a member of WinNT Usergroup in VB.Net

How to determine if a UserID is a member of WinNT Usergroup in VB.Net

Scheduled Pinned Locked Moved Visual Basic
csharptutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qnguyen
    wrote on last edited by
    #1

    How to determine a UserID (in Windows) is a member of a certain UserGroup (like SalesManagers) in VB.Net? I tried DirectoryEntry and DirectorySearcher in the System.DirectoryServices name spaces, but I cannot get it to work. I also tried WindowsIdentity and WindowsPrincipal.IsInRole but with no correct result. I cannot find any sample codes. Any suggestion? Thanks

    R 1 Reply Last reply
    0
    • Q qnguyen

      How to determine a UserID (in Windows) is a member of a certain UserGroup (like SalesManagers) in VB.Net? I tried DirectoryEntry and DirectorySearcher in the System.DirectoryServices name spaces, but I cannot get it to work. I also tried WindowsIdentity and WindowsPrincipal.IsInRole but with no correct result. I cannot find any sample codes. Any suggestion? Thanks

      R Offline
      R Offline
      Ray Cassick
      wrote on last edited by
      #2

      Here is the function that I have for a project that I was working on. Good luck...

      Imports System.DirectoryServices

      Friend Function VerifyUser(ByVal Username As String, ByVal Password As String, ByVal groupType As EGroupType) As Boolean

          Dim RootDomain As New DirectoryEntry("LDAP://rootDSE")
          Dim strDNC As String = RootDomain.Properties("DefaultNamingContext")(0)
          Dim UserEntry As New DirectoryEntry("LDAP://" & strDNC, Username, Password)
          Dim Searcher As DirectorySearcher = New DirectorySearcher(UserEntry)
          Dim SearchResults As SearchResultCollection
          Dim SearchResult As SearchResult
          Dim GroupPresentFlag As Boolean
          Dim Data As String
          Dim Cnt As Integer
          Dim ResultCollection As ResultPropertyCollection
          Dim ValueCollection As ResultPropertyValueCollection
          Dim m\_configSettings As New GlobalData()
      
          Username = Username.Trim(" ")
          Password = Password.Trim(" ")
      
          Searcher.Filter = ("(samaccountname= " & Username & ")")
          Searcher.PropertiesToLoad.Add("memberOf")
          GroupPresentFlag = False
      
          Try
              SearchResults = Searcher.FindAll
      
          Catch Ex As Exception
      
          End Try
      
      
          For Each SearchResult In SearchResults
              ResultCollection = SearchResult.Properties
      
          Next
      
          ValueCollection = ResultCollection.Item("memberOf")
          For Cnt = 0 To ValueCollection.Count - 1
              Data = CType(ValueCollection(Cnt), String)
              Select Case groupType
                  Case EGroupType.Users
                      If InStr(Data, m\_configSettings.ADUserGroupName, CompareMethod.Text) Then
                          GroupPresentFlag = True
      
                      End If
      
                  Case EGroupType.Poweruser
                      If InStr(Data, m\_configSettings.ADPoweruserGroupName, CompareMethod.Text) Then
                          GroupPresentFlag = True
      
                      End If
      
                  Case EGroupType.EarlyAdopter
                      If InStr(Data, m\_configSettings.ADEarlyadopterGroupName, CompareMethod.Text) Then
                          GroupPresentFlag = True
      
                      End If
      
                  Case EGroupType.Admin
                      If InStr(Data, m\_configSettings.ADAdminGroupName, CompareMethod.Text) Then
                          GroupPresentFlag = True
      
                      End If
      
              End Select
      
          Next
      
          If (Not m\_configSettings
      
      Q 1 Reply Last reply
      0
      • R Ray Cassick

        Here is the function that I have for a project that I was working on. Good luck...

        Imports System.DirectoryServices

        Friend Function VerifyUser(ByVal Username As String, ByVal Password As String, ByVal groupType As EGroupType) As Boolean

            Dim RootDomain As New DirectoryEntry("LDAP://rootDSE")
            Dim strDNC As String = RootDomain.Properties("DefaultNamingContext")(0)
            Dim UserEntry As New DirectoryEntry("LDAP://" & strDNC, Username, Password)
            Dim Searcher As DirectorySearcher = New DirectorySearcher(UserEntry)
            Dim SearchResults As SearchResultCollection
            Dim SearchResult As SearchResult
            Dim GroupPresentFlag As Boolean
            Dim Data As String
            Dim Cnt As Integer
            Dim ResultCollection As ResultPropertyCollection
            Dim ValueCollection As ResultPropertyValueCollection
            Dim m\_configSettings As New GlobalData()
        
            Username = Username.Trim(" ")
            Password = Password.Trim(" ")
        
            Searcher.Filter = ("(samaccountname= " & Username & ")")
            Searcher.PropertiesToLoad.Add("memberOf")
            GroupPresentFlag = False
        
            Try
                SearchResults = Searcher.FindAll
        
            Catch Ex As Exception
        
            End Try
        
        
            For Each SearchResult In SearchResults
                ResultCollection = SearchResult.Properties
        
            Next
        
            ValueCollection = ResultCollection.Item("memberOf")
            For Cnt = 0 To ValueCollection.Count - 1
                Data = CType(ValueCollection(Cnt), String)
                Select Case groupType
                    Case EGroupType.Users
                        If InStr(Data, m\_configSettings.ADUserGroupName, CompareMethod.Text) Then
                            GroupPresentFlag = True
        
                        End If
        
                    Case EGroupType.Poweruser
                        If InStr(Data, m\_configSettings.ADPoweruserGroupName, CompareMethod.Text) Then
                            GroupPresentFlag = True
        
                        End If
        
                    Case EGroupType.EarlyAdopter
                        If InStr(Data, m\_configSettings.ADEarlyadopterGroupName, CompareMethod.Text) Then
                            GroupPresentFlag = True
        
                        End If
        
                    Case EGroupType.Admin
                        If InStr(Data, m\_configSettings.ADAdminGroupName, CompareMethod.Text) Then
                            GroupPresentFlag = True
        
                        End If
        
                End Select
        
            Next
        
            If (Not m\_configSettings
        
        Q Offline
        Q Offline
        qnguyen
        wrote on last edited by
        #3

        Thanks Ray for your sample. I just found out that my company does not have LDAP yet. Only Windows 2000. So, I guess I cannot use the DirectorySearcher in this case. What is your comment on this? Am I out of luck? Thanks.

        R 1 Reply Last reply
        0
        • Q qnguyen

          Thanks Ray for your sample. I just found out that my company does not have LDAP yet. Only Windows 2000. So, I guess I cannot use the DirectorySearcher in this case. What is your comment on this? Am I out of luck? Thanks.

          R Offline
          R Offline
          Ray Cassick
          wrote on last edited by
          #4

          Are they using Active Directory? That is all that I am using.


          Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Jörgen Sigvardsson wrote: If the physicists find a universal theory describing the laws of universe, I'm sure the asshole constant will be an integral part of that theory.


          Q 1 Reply Last reply
          0
          • R Ray Cassick

            Are they using Active Directory? That is all that I am using.


            Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Jörgen Sigvardsson wrote: If the physicists find a universal theory describing the laws of universe, I'm sure the asshole constant will be an integral part of that theory.


            Q Offline
            Q Offline
            qnguyen
            wrote on last edited by
            #5

            Yes. But the path would be like this "WinNT://DomainName/GroupName". I tried the following codes and I can see the GroupName there but I cannot see the members contain inside the group. Dim DEntry As New DirectoryServices.DirectoryEntry("WinNT://DomainName") Dim UserGrp As DirectoryServices.DirectoryEntry For Each UserGrp In DEntry.Children If UserGrp.Path = "WinNT://DomainName/GroupName" Then MessageBox.Show(UserGrp.Path) End If Next There, you can try it out. Again, many thanks, Ray.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups