Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find out if a user is in a group 2

Status
Not open for further replies.

Blitz

Technical User
Jul 7, 2000
171
US
i found this sample that checks the current user and tells if they are part of the admin group or guest group but it only checks the local machine. How would i make it check our server and find if the current user is part of a group on the server? thanks for any help.

Imports System
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Principal

<Assembly: SecurityPermission(SecurityAction.RequestMinimum, ControlPrincipal:=True)>

Namespace SecuritySamples

Public Class IdentityCheck

Public Shared Sub Main()

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)

Dim user As WindowsPrincipal = CType(System.Threading.Thread.CurrentPrincipal, WindowsPrincipal)

Console.WriteLine(&quot;User name: {0}&quot;, user.Identity.Name)
Console.WriteLine(&quot;Authentication type: {0}&quot;, user.Identity.AuthenticationType)
Console.WriteLine(&quot;Is in Administrators group: {0}&quot;, user.IsInRole(WindowsBuiltInRole.Administrator))
Console.WriteLine(&quot;Is in Guests group: {0}&quot;, user.IsInRole(windowsbuiltinrole.Guest))


Try
Console.WriteLine()
Console.WriteLine(&quot;{0}&quot;, DeclAdminCheck())
Catch e As Exception
If (TypeOf e Is SecurityException) And (CType(e, SecurityException).PermissionType Is GetType(PrincipalPermission)) Then
Console.WriteLine(&quot;Declarative check for Administrators group failed!&quot;)
Else
Console.WriteLine(&quot;Exception occurred: {0}&quot;, e)
End If
End Try

Try
Console.WriteLine()
Console.WriteLine(&quot;{0}&quot;, DeclGuestCheck())
Catch e As Exception
If (TypeOf e Is SecurityException) And (CType(e, SecurityException).PermissionType Is GetType(PrincipalPermission)) Then
Console.WriteLine(&quot;Declarative check for Guests group failed!&quot;)
Else
Console.WriteLine(&quot;Exception occurred: {0}&quot;, e)
End If
End Try

Console.WriteLine()
Console.Write(&quot;Press Enter to exit...&quot;)
Console.Read()

End Sub 'Main

<PrincipalPermission(SecurityAction.Demand, Role:=&quot;BUILTIN\Administrators&quot;)> Private Shared Function DeclAdminCheck() As String

Return &quot;Declarative check for Administrators group passed!&quot;

End Function 'DeclAdminCheck

<PrincipalPermissionAttribute(SecurityAction.Demand, Role:=&quot;BUILTIN\Guests&quot;)> Private Shared Function DeclGuestCheck() As String

Return &quot;Declarative check for Guests group passed!&quot;

End Function 'DeclGuestCheck

End Class 'IdentityCheck

End Namespace 'SecuritySamples

 
Do you have Active Directory installed?

If so, you can use the classes in the System.DirectoryServices namespace. See the DirectorySearcher class, plus you'll probably need a book or two on how AD works.

Chip H.
 
If you can live without 'option strict' (and you've got AD as chiph says, you can use the VB6 way:
--------------------------------------------------------
'From Function IsMember(ByVal strDomain As String, ByVal strGroup _
As String, ByVal strMember As String) As Boolean
Dim grp As Object
Dim strPath As String

strPath = &quot;WinNT://&quot; & strDomain & &quot;/&quot;
grp = GetObject(strPath & strGroup & &quot;,group&quot;)
IsMember = grp.IsMember(strPath & strMember)
End Function
--------------------------------------------------------

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Thank you both, I will be reading up on AD and see if i want to continue to go this route or find another solution, thanks
 
I have the exact same problem as Blitz and this is the only post I could find about it.

I am trying to find some sample code that takes a domain and username and determines if the user is in a specific windows group. I am not sure what AD is, but from what I have read so far, it seems that it should be possible with .NET 1.1.

If you have any samples or pointers, I would greatly appreciate it ;-)
 
Ok, I finally think I got this figured out. I used
the below code to return a boolean value to indicate if the user is apart of a windows group. It seems to work ok!

Imports System.Security.Principal
Imports System.Threading

Sub main()
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)

MsgBox(Thread.CurrentPrincipal.IsInRole("HNCORP\Tech_Admin"))

End Sub
 
From what I remember the IsInRole will only work for groups on the local machine, You cannot check the role on the server with it.
 
No, this actually works great. I have tested it and it checks the role on the server. I think it is a great find since I couldn't find anything for days!

The msgbox will return a true or false value. That can be assigned to variable.

 
since you can't give yourself a star (I wonder why), I will.

and could you be so kind to make it a faq.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top