Solved

IsInRole says I'm a User, not an Administrator

Posted on 2011-09-27
6
646 Views
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?
0
Comment
Question by:ElrondCT
  • 3
  • 3
6 Comments
 
LVL 40

Accepted Solution

by:
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).

0
 
LVL 20

Author Comment

by:ElrondCT
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)?
0
 
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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 20

Author Comment

by:ElrondCT
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.
0
 
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:

System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)

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.
0
 
LVL 20

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now