Solved

C# File permissions in Vista

Posted on 2008-10-21
3
2,423 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
Comment Utility
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
Comment Utility
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
Comment Utility
The only one that does work is if I put

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

13 Experts available now in Live!

Get 1:1 Help Now