• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Question UAC Elevation

PhilG

Member
Joined
Jan 30, 2017
Messages
5
Programming Experience
Beginner
(Beginner) hello,

now that the formalities are out of the way, i was wondering if any of you here have any experience of UAC.

my reasons for asking are as follows. im working on a small FSU(Field Service Utility) to aide us field engineers with a client we have, problem i got it the clients systems run EnabledLUA to to 0 - effectivley disabled.

i can run the app with admin rights if i left shift and right click the app and select run as different user, and so far im managing this way but sort of defies the point of it.

so my question is how do i force the Runas or UAC prompts with uac disabled

i already have this in my program.cs

Code:
static void Main()
        {
            WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);


            if (!hasAdministrativeRight)
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.UseShellExecute = true;
                startInfo.WorkingDirectory = Environment.CurrentDirectory;
                startInfo.FileName = Application.ExecutablePath;
                startInfo.Verb = "runas";
                try
                {
                    Process p = Process.Start(startInfo);
                    Application.Exit();
                }
                catch (System.ComponentModel.Win32Exception ex)
                {
                    MessageBox.Show("This utility requires elevated priviledges to complete correctly.", "Error: UAC Authorisation Required", MessageBoxButtons.OK);
                    //Debug.Print(ex.Message);
                    return;
                }
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
but with uac disabled this cause the program to hang on load and if i check task manager i can see the app closing and reopening constantly with only one way to stop it which is logout/rebboot

any help appreciated

thanks

phil

p.s forgot to add

i already have requireAdministrator in my app manifest file
 
Last edited:

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
498
Location
Norway
Programming Experience
10+
i already have requireAdministrator in my app manifest file
That and "runas" are UAC featurer, without UAC those will do nothing. This is for legacy systems where UAC can actually be turned off, in newer systems only notification can be turned off and those UAC elevations should happen silently.

You can set UserName and Password (UseShellExecute:false) for ProcessStartInfo to an admin account. (this implies "run as different user")

if i check task manager i can see the app closing and reopening constantly
This should be avoided by your code, for example store a value for restart attempts and check this value before trying to restart yet again.
 

PhilG

Member
Joined
Jan 30, 2017
Messages
5
Programming Experience
Beginner
nm resolved, ill show workings here later to help others
 
Last edited:

PhilG

Member
Joined
Jan 30, 2017
Messages
5
Programming Experience
Beginner
thanks john for you ideas, i have how ever done it a different way this is as follows i hope its of help to some one as ive been struggling to sort this for ages, this is placed in the program.cs

Code:
        static void Main(string[] args)        {


            if (IsAdministrator() == false)
            {
                try
                {
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.Domain = "Domain_Here";
                    p.StartInfo.UserName = "Username_Here";
                    p.StartInfo.FileName = "Filename.exe";
                    //p.StartInfo.Arguments = "";
                    System.String rawPassword = "Password_Here";
                    System.Security.SecureString encPassword = new System.Security.SecureString();
                    foreach (System.Char c in rawPassword)
                    {
                        encPassword.AppendChar(c);
                    }


                    p.StartInfo.Password = encPassword;
                    p.StartInfo.UseShellExecute = false;
                    p.Start();
                    Environment.Exit(0);
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error");
                }
            }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        private static bool IsAdministrator()
        {
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
 
Top Bottom