Checking if Administrator

  • Thread starter Thread starter henrycortezwu
  • Start date Start date
H

henrycortezwu

Hi All,

Quick question, I manage to come up with a working code the
successfully checks if the current users is part of the Administrators
group or not. My question is, will this code work from Windows 95? 98?
ME? 2000?

My development machine is currently XP, and it works fine here.

Thanks in advance,
Henry :)

Private Function CheckAdministratorRole() As Boolean
On Error Resume Next


System.AppDomain.CurrentDomain.SetPrincipalPolicy(Security.Principal.PrincipalPolicy.WindowsPrincipal)
Dim prpAdministrator As New
System.Security.Permissions.PrincipalPermission(Environment.UserName,
"BUILTIN\Administrators")

prpAdministrator.Demand()

If Err.Number <> 0 Then
CheckAdministratorRole = True
Else
CheckAdministratorRole = False
End If
End Function
 
Oh by the way, which is the better code, the 1st one i posted or the
ff:

Private Function CheckAdminRole() As Boolean
Dim myDomain As AppDomain = System.Threading.Thread.GetDomain()


myDomain.SetPrincipalPolicy(Security.Principal.PrincipalPolicy.WindowsPrincipal)
Dim myPrincipal As Security.Principal.WindowsPrincipal =
CType(System.Threading.Thread.CurrentPrincipal,
Security.Principal.WindowsPrincipal)

If myPrincipal.IsInRole("BUILTIN\Administrators") = True Then
CheckAdminRole = True
Else
CheckAdminRole = False
End If
End Function

It works the same, but what about scenarios on a user belonging to two
or more domains? Which is the better code?
And lastly, on my 1st code, what does the
"System.AppDomain.CurrentDomain­.SetPrincipalPolicy(Security.Principal.PrincipalPolicy.Windo­wsPrincipal)"
do? When I commented it out, the code still works.

Thanks again,
Henry :)
 
Hi All,

Quick question, I manage to come up with a working code the
successfully checks if the current users is part of the Administrators
group or not. My question is, will this code work from Windows 95? 98?
ME? 2000?

My development machine is currently XP, and it works fine here.

Thanks in advance,
Henry :)

As I recall, Windows 95, 98 and ME don't have the same security model as
NT4/2000/XP and don't have the concept of an administrator (nor of a
domain).

Your code should therefore work on NT4 and 2000. I haven't a clue what it
will do on 95/98/ME (not having tried it), but its possible it will degrade
gracefully ...? Hopefully someone with 95/98/ME will try it for you.

Brian.

www.cryer.co.uk/brian
 
Quick question, I manage to come up with a working code the
successfully checks if the current users is part of the Administrators
group or not. My question is, will this code work from Windows 95? 98?
ME? 2000?
[...]
Private Function CheckAdministratorRole() As Boolean
On Error Resume Next


System.AppDomain.CurrentDomain.SetPrincipalPolicy(Security.Principal.PrincipalPolicy.WindowsPrincipal)
Dim prpAdministrator As New
System.Security.Permissions.PrincipalPermission(Environment.UserName,
"BUILTIN\Administrators")

prpAdministrator.Demand()

If Err.Number <> 0 Then
CheckAdministratorRole = True
Else
CheckAdministratorRole = False
End If
End Function

MSDN on 'IPrincipal.IsInRole' and 'AppDomain.SetPrincipalPolicy' says:

| *Requirements*
|
| *Platforms:* Windows 98, Windows NT 4.0, Windows
| Millennium Edition, Windows 2000, Windows XP Home Edition,
| Windows XP Professional, Windows Server 2003 family

This means that the methods are supported by all versions of Windows running
the .NET Framework. The .NET Framework doesn't work together with Windows
95. I suggest to check the documentation for the requirements of other
methods you are using too.

Simpler solution for checking if the user is an administrator:

\\\
Imports System.Security.Principal
..
..
..
Dim wp As New WindowsPrincipal(WindowsIdentity.GetCurrent())
Dim IsAdmin As Boolean = wp.IsInRole(WindowsBuiltInRole.Administrator)
///
 
Herfried,

I forgot to look at the bottom, good that you did that

:-)

Cor
 
Thanks guys!

98 is ok to me!! :) Clients are up to 98!

Thanks again for the shorter code!!

Henry :)
Quick question, I manage to come up with a working code the
successfully checks if the current users is part of the Administrators
group or not. My question is, will this code work from Windows 95? 98?
ME? 2000?
[...]
Private Function CheckAdministratorRole() As Boolean
On Error Resume Next


System.AppDomain.CurrentDomain.SetPrincipalPolicy(Security.Principal.PrincipalPolicy.WindowsPrincipal)
Dim prpAdministrator As New
System.Security.Permissions.PrincipalPermission(Environment.UserName,
"BUILTIN\Administrators")

prpAdministrator.Demand()

If Err.Number <> 0 Then
CheckAdministratorRole = True
Else
CheckAdministratorRole = False
End If
End Function

MSDN on 'IPrincipal.IsInRole' and 'AppDomain.SetPrincipalPolicy' says:

| *Requirements*
|
| *Platforms:* Windows 98, Windows NT 4.0, Windows
| Millennium Edition, Windows 2000, Windows XP Home Edition,
| Windows XP Professional, Windows Server 2003 family

This means that the methods are supported by all versions of Windows running
the .NET Framework. The .NET Framework doesn't work together with Windows
95. I suggest to check the documentation for the requirements of other
methods you are using too.

Simpler solution for checking if the user is an administrator:

\\\
Imports System.Security.Principal
.
.
.
Dim wp As New WindowsPrincipal(WindowsIdentity.GetCurrent())
Dim IsAdmin As Boolean = wp.IsInRole(WindowsBuiltInRole.Administrator)
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top