?
Solved

C# File permissions in Vista

Posted on 2008-10-21
3
Medium Priority
?
2,525 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

752 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