Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


IsInRole says I'm a User, not an Administrator

Posted on 2011-09-27
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 2000 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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

670 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