erenjs
erenjs

Reputation: 75

C# released exe won't start on non-admin accounts

So I've created a GUI which activates and deactives network adapters using impersonation and powershell commands.

Here is a snippet from the code:

Get_adapters gets all adapters and bind them to the listview:

    public MainWindow()
    {
        InitializeComponent();
        Get_adapters();
    }

These are my two button commands:

    private void Activate_interface(object sender, RoutedEventArgs e)
    {
        string interface_name = ((Netadapter)ListView.SelectedItem).Name;
        Change_adapter_status(true, interface_name);
    }

    private void Deactivate_interface(object sender, RoutedEventArgs e)
    {
        string interface_name = ((Netadapter)ListView.SelectedItem).Name;
        Change_adapter_status(false, interface_name);
    }

This is the powershell method: (Account credentials hidden; but I've cheked them multiple times and they're correct).

    public void Change_adapter_status(bool _option, string _interface)
    {
        using (new Impersonator("Administrator", "DOMAIN", "pw"))
        {
            var process_info = new ProcessStartInfo();
            process_info.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
            process_info.Verb = "runas";

            if (_option == true)
            {
                process_info.Arguments = String.Format("netsh interface set interface „{0}“ enable", _interface);
            }
            else if (_option == false)
            {
                process_info.Arguments = String.Format("netsh interface set interface „{0}“ disable", _interface);
            }
            Process.Start(process_info);
        }
    }

As you can see, I use impersonation to start a powershell window as an admin user.

On my machine (Admin acc) everything works as excepted. Button is clicked, powershell window opens and does its thing.

On other computers (non-admins BUT are in the same domain, and have access to the admin account with the credentials from code above) the exe won't even start.

The computers have the same images (Windows 10) and the targeted .net framework is also correct.

On other admin accounts the exe is executable. So what could it be that the exe won't start for non-admin users? Thanks is advance.

Event output: - System

Upvotes: 0

Views: 100

Answers (3)

erenjs
erenjs

Reputation: 75

So this is how I ended up doing it: I removed the impersonation part of the code and replaced it with the inbuild methods from Process.StartInfo(); This is part of a gui to configure network adapters for non-admin users. Feel free to use.

public void Change_adapter_status(bool _option, string _interface)
        {
            SecureString password = new SecureString();
            password.AppendChar('p');
            password.AppendChar('a');
            password.AppendChar('s');
            password.AppendChar('s');
            password.AppendChar('w');
            password.AppendChar('o');
            password.AppendChar('r');
            password.AppendChar('d');

            Process p = new Process();
            p.StartInfo.UserName = "Administrator";
            p.StartInfo.Domain = "domain";
            p.StartInfo.Password = password;
            p.StartInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
            p.StartInfo.Verb = "runas";
            p.StartInfo.UseShellExecute = false;

            if (_option == true)
            {
                p.StartInfo.Arguments = String.Format("netsh interface set interface „{0}“ enable", _interface);
            }
            else if (_option == false)
            {
                p.StartInfo.Arguments = String.Format("netsh interface set interface „{0}“ disable", _interface);
            }
            p.Start();
            p.WaitForExit();
            Get_adapters(true);
        }

Upvotes: 0

user11909
user11909

Reputation: 1265

You have to use WindowsIdentity.Impersonate Method to impersonate.

Upvotes: 0

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23228

Try to add app.manifest file to project with your executable and set <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> on it

Upvotes: 1

Related Questions