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

C#:
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:
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.
 
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

C#:
        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);
        }
 
Back
Top Bottom