Solved

C# File permissions in Vista

Posted on 2008-10-21
3
2,513 Views
Last Modified: 2013-12-17
I am having a few problems with permissions in windows Vista using C# code from an MCTS book.  It is a simple calculator program with some of the buttons restricted depending on the Role of the user.  My current user is set to Administrator under vista.  The only anyoying problem I have is that I have to confirm to delete or copy something in the file system.  Other than that I seem to have full control over the system.  However, when I run the code

public Form1()
      {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();
            WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();

            WindowsPrincipal currentPrincipal = new
                WindowsPrincipal(currentIdentity);

            System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

            if (!currentPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                subtractButton.Visible = false;
                multiplyButton.Visible = false;
            }
            //This shows however throws System.Security.SecurityException
            if (!currentPrincipal.IsInRole(WindowsBuiltInRole.User))
            {
                addButton.Visible = false;
            }
            //This is the only one that works
            if (!(currentIdentity.Name.ToLower() ==
                System.Environment.MachineName.ToLower() + @"\neville"))
                divideButton.Visible = false;
            }

only the addButton and divideButton show.  The divide button is ok when I try the calculator program it calculates fine.  The add button has the code


        private void addButton_Click(object sender, System.EventArgs e)
        {
            try
            {
                PrincipalPermission userPermission =
                    new PrincipalPermission(null, @"Neville-Pc\Users");
                userPermission.Demand();
                MessageBox.Show("Access Granted");
               
                int answer = (int.Parse(integer1.Text) + int.Parse(integer2.Text));
                answerLabel.Text = answer.ToString();
            } catch (System.Security.SecurityException ex) {
                MessageBox.Show("Access denied: " + ex.Message);
            }
        }

And it throws an exception Access Denied.  

I'm not sure why this would happen because on my vista account I have Administrator under my name.  So why wouldn't I have the privilege to
1. have the subtract and multiply buttons via the condition
 if (!currentPrincipal.IsInRole(WindowsBuiltInRole.Administrator))

2.  I am a "user" on my machine so why won't the add code run but the button is visible via the condition
if (!currentPrincipal.IsInRole(WindowsBuiltInRole.User))

I'm not understanding security with C# .Net 2.0 and Windows Vista.  Is there some setting that I have to click to get everything working.  Or maybe I am not the Administrator even though it says I am under Control Panel -> User Accounts, any way of really checking?

Thanks
0
Comment
Question by:Neville_Chinan
[X]
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
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 22773554
try disabling the user account control for vista users
from the following link user method 4 for doing it and se whether your problem goes away or not
http://www.petri.co.il/disable_uac_in_windows_vista.htm
0
 

Author Comment

by:Neville_Chinan
ID: 22779485
That worked and it showed all the buttons, however The code

            if (!currentPrincipal.IsInRole(WindowsBuiltInRole.User))
            {
                addButton.Visible = false;
            }

Shows the button

However the code

        private void addButton_Click(object sender, System.EventArgs e)
        {
            string allowUser = System.Environment.MachineName + @"\Users";
            try
            {
                PrincipalPermission userPermission =
                    new PrincipalPermission(allowUser, null);
                userPermission.Demand();
               
                int answer = (int.Parse(integer1.Text) + int.Parse(integer2.Text));
                answerLabel.Text = answer.ToString();
            } catch (System.Security.SecurityException ex) {
                MessageBox.Show("Access denied: " + ex.Message);
            }
        }

Throws an excpetion, why is that?  The group Users and Administrators do exist I checked and I am an administrator and user so why doesn't this code work?  I think I might be missing something out.
also
string allowUser = System.Environment.MachineName + @"\Administrators";
throws an exception to in the same addButton_Click
Thanks
0
 

Author Comment

by:Neville_Chinan
ID: 22779492
The only one that does work is if I put

string allowUser = System.Environment.MachineName + @"\Neville";
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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