Solved

C# File permissions in Vista

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

16 Experts available now in Live!

Get 1:1 Help Now