By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,481 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,481 IT Pros & Developers. It's quick & easy.

Checking if Administrator

P: n/a
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.WindowsPrincipa l)
Dim prpAdministrator As New
System.Security.Permissions.PrincipalPermission(En vironment.UserName,
"BUILTIN\Administrators")

prpAdministrator.Demand()

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

Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies

P: n/a
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.Pri ncipalPolicy.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*.SetPrincipalPolic y(Security.Principal.PrincipalPolicy.Windo*wsPrinc ipal)"
do? When I commented it out, the code still works.

Thanks again,
Henry :)

Nov 21 '05 #2

P: n/a
Henry,

Surely not on Windows'95 when you use VBNet, I never heard that it was not
working on W98/me and the others. However why would it not, it is a dotNet
part.

The only restriction I see here.
http://msdn.microsoft.com/library/de...10security.asp

However I did not test it so I can be wrong.

Cor


Nov 21 '05 #3

P: n/a
<he***********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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
Nov 21 '05 #4

P: n/a
<he***********@gmail.com> schrieb:
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.WindowsPrincipa l)
Dim prpAdministrator As New
System.Security.Permissions.PrincipalPermission(En vironment.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)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

P: n/a
Herfried,

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

:-)

Cor
Nov 21 '05 #6

P: n/a
Thanks guys!

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

Thanks again for the shorter code!!

Henry :)

Herfried K. Wagner [MVP] wrote:
<he***********@gmail.com> schrieb:
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.WindowsPrincipa l)
Dim prpAdministrator As New
System.Security.Permissions.PrincipalPermission(En vironment.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)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.