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
Solved

IsInRole says I'm a User, not an Administrator

Posted on 2011-09-27
6
674 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Write to a printer using vb.net 9 38
How to open form using item in Listbox. 8 23
Complex SQL statement in VB.NET 7 31
Very Large data in MYSQL 7 73
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

792 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