How to determine if a UserID is a member of WinNT Usergroup in VB.Net
-
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
-
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
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
-
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
-
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.
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.
-
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.
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.