Solved

C# File permissions in Vista

Posted on 2008-10-21
3
2,479 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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