IsInRole says I'm a User, not an Administrator

Posted on 2011-09-27
Last Modified: 2012-05-12
I want to put code into my VB 2008 app to see whether the user is an Administrator or not. I've tried three different methods, and all are reporting that I'm not an Administrator (I'm just a User), even though Control Panel/Users clearly reports that I'm an Administrator. Further testing indicates that I get this result on Windows 7 (I've checked two different machines, one Win7 Home Premium and the other Win7 Pro), but with XP I get the proper Administrator response.

I've tried the following methods of checking (based on different suggestions I've seen on EE):

1) my.User.IsInRole(ApplicationServices.BuiltInRole.Administrator)

2)  Dim idMe As WindowsIdentity = WindowsIdentity.GetCurrent
     Dim prinMe As New WindowsPrincipal(idMe)
     If prinMe.IsInRole(WindowsBuiltInRole.Administrator) Then

3)     AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
        Dim WP As WindowsPrincipal
        WP = System.Threading.Thread.CurrentPrincipal
        If WP.IsInRole(WindowsBuiltInRole.Administrator) Then
Everything is reporting False. They all report true for WindowsBuiltInRole.User. Any ideas on why? Is there something different I should be doing on Win7?
Question by:ElrondCT
  • 3
  • 3
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 500 total points
ID: 36713211
In Windows 7, an application always start under User privileges, even if the current user is an Administrator. The methods you used all reflect this.

In order to be able to detect if the use is administrator, the application should be started in administator mode, either by setting that condition in the shortcut properties, launching the application through the Run As Admistrator option of its contex menu, or by requiring administrator privileges under the UAC settings through the View Windows Settings of the Application tab of the project's Properties window.

Even there, my.user wont give you the right answer. The whole My namespace contains limited capacity versions of other namespaces in the framework.

Code 2) works when the application is started in Administrator mode. I haven't checked code 3).

LVL 20

Author Comment

ID: 36713389
Thanks for the information. This is obviously more complicated than I thought. I'm going to need to think through and test how the different methodology works with some of the things my software is doing (all the while remembering that XP is different, since I have clients using both). Do you know where Vista fits in this (like 7 or like XP)?
LVL 40
ID: 36713461
Vista is like Windows 7... or the reverse.

When Vista came along, they changed a lot of things security wise. That thing about Administrator vs User is one of the important ones.

Applications always start with User privileges even when run by an administrator.

But the other way around, an administrator can get into a session run by a regular user and start any application under his administrator credentials with the techniques I mentioned.

Security is increased because the administrator always have to give his password when running an application that needs administrator privileges. And this makes things a lot more interesting for the administrator, because he does not have to log off a regular user account in order to make administrative changes to a computer.

But for us, poor programmers, lots a new things to learn. You will find out about those differences as you go along, such as the fact that Documents and Settings is now called Users.

There are usually ways to easily overcome those differences, but that is something you have to learn to live with when you are dealing with multiple OS with the same application.
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 20

Author Comment

ID: 36714225
Yes, I'm aware of the redesign of the folders. Fortunately, VB makes some of that simpler because My.Computer.FileSystem.SpecialDirectories will automatically give the correct folder location, no matter which version of Windows.

Well, I can see I've got some unexpected additional work to do. Thanks for the guidance.
LVL 40
ID: 36714290
Just another piece of guidance. Try to stay away of My, except for My.Resources and My.Settings.

My is just shortcuts to other classes in the framework. It has been conceived to help amateurs. They can find most of what they need to do with what they find in My. It might make things easier in the beginning, but it hides a lot of possibilities from you.

As an example, My.Computer.FileSystem has about 30 methods to work with files, directories and drives. System.IO.File as more than that only for files.

If you ever move to another language and are used to My, you will have problem switching because My is there only for VB. The framework classes and methods on the other hand are the same for all languages.

And most of the things you find there are as easy to make using the framework, you just have to look around a little more to find the namespace that contains the feature, such as the following that you will probably recognize:


Once you have found the namespace for special folders, you will also find a nice treasure under System.Environment, some you get in My, but a lot that are not there.
LVL 20

Author Comment

ID: 36714304
OK. Thanks. I've always hated the "My" terminology anyway, though it's disgustingly ubiquitous in computer usage these days...

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question