-1

Account A is not an administrator, so log in to the WINDOWS system with account A

Account B has administrator authority.

Functions that need to be implemented: I use account A to log in to the windows system and execute the program. At this time, it prompts that I need to execute it with administrator privileges, but I have the administrator user name and password. I need the program to automatically log in and run the program with my administrator account.

But now I use the built-in B account in the program code. After running, the program keeps creating new processes and enters an endless loop. Why?

Porgrams.cs


 public static class Program
    {
        /// <summary>
        /// 
        /// </summary>
        [STAThread]
        static void Main()
        {

            if (!IsRunAsAdmin())
            {
                RestartAsAdmin();
                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        private static bool IsRunAsAdmin()
        {
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        private static void RestartAsAdmin()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.UseShellExecute = false;
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            startInfo.FileName = Application.ExecutablePath;
            startInfo.UserName = "li"; // administrator account
            startInfo.Password = CreateSecureString("123"); // administrator password
            startInfo.Verb = "runas"; 

            try
            {
                Process.Start(startInfo);
            }
            catch (System.ComponentModel.Win32Exception)
            {
                return;
            }

            Application.Exit();
        }

        private static SecureString CreateSecureString(string str)
        {
            SecureString secureString = new SecureString();
            foreach (char c in str)
            {
                secureString.AppendChar(c);
            }
            return secureString;
        }

    }
gong go
  • 1
  • 3
  • https://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator – Hans Passant Jul 14 '23 at 16:10
  • I have an administrator account I want the application to run under administrator privileges, but I don't want the user to enter the administrator account when prompted by UAC – gong go Jul 15 '23 at 14:01
  • 1
    That's a basic misunderstanding of UAC. It is not made to stop your program from running, it is all about notifying the user that a potentially risky program tries to run. You cannot bypass that notification. A sensible alternative is to let the risky stuff be done by a service. Still requires UAC elevation to install the service, but is done only once. – Hans Passant Jul 15 '23 at 14:25
  • The need now is We are an internal system, ordinary people do not have administrator rights But when you need to execute the program, you need to have administrator privileges to execute it, but at the same time, you don’t want ordinary people to know the administrator account and password. You want to skip the UAC prompt in the program. – gong go Jul 17 '23 at 02:23

1 Answers1

1

Starting with Windows Vista, processes no longer start as an administrator by default, even if the user account has administrator rights. To get a process with administrator permissions, you must deliberately use the "Run as Administrator" option.

This means subsequent if (!IsRunAsAdmin()) checks fail, even though they are run with the proper user credentials, and the application will keep restarting.

There is no way to avoid a UAC prompt to grant the required administrator access without actually disabling UAC. The only work-around I know for this is using a Scheduled Task.

The better strategy is to identify the specific tasks in the program that need administrator credentials and either adjust the application so this is no longer required (for example: write the Application Data folder instead of the Program Files folder), or isolate those specific tasks to a separate application from the main forms application, so you can start them in their own dedicated process with the required UAC prompt at just those times.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Now, people who use computers do not have administrator rights, but the programs we develop ourselves need to use administrator rights to execute on the client computer, but we don't want users to enter the administrator account and password from UAC. So it needs to be built in the program to call the administrator account. Is there any other way to solve this problem? – gong go Jul 15 '23 at 00:52
  • Yes. Find ways not to need admin. – Joel Coehoorn Jul 16 '23 at 02:58
  • But I haven't found a good and effective way to skip the UAC prompt and build the administrator account into the program so that the program can be executed with administrator privileges – gong go Jul 17 '23 at 02:24
  • If there were a way to skip UAC prompts, it would remove the whole point of having them. **You will not be able to do that.** But you misunderstand my suggestion. Whatever the program is doing that needs admin rights, there's almost always a way to _change the app's behavior_ so you still accomplish the goal without needing admin rights in the first place. – Joel Coehoorn Jul 17 '23 at 13:26