1

I have a wcf web service that I am running via console. If I start the service as an admin i.e. right click the executable an run as administrator then it works, but if I just open it it complains and says "http could not register url http //+ 80/ . . .". Alternatively I could run netsh within my c# code and add the URL (I suppose), but that too needs admin privileges. I tried: this code , GetIP() obtains the string literal of the ip address v4

ProcessStartInfo procInfo = new ProcessStartInfo
{
    WorkingDirectory = Path.GetPathRoot(Environment.SystemDirectory),
    FileName = Path.Combine(Environment.SystemDirectory, "netsh.exe"),
    Arguments = "netsh http add urlacl url=http://" + GetIP() + ":80/VSPDataLogger/ user=everyone",
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false,
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden
};

Process proc = Process.Start(procInfo);
proc.WaitForExit();

It would be nice if I can start the application without prompting the user to start as admin (UAC nonsense). I tried adding the manifest file and changing the execution level to admin but that didn't seem to work or my application is ignoring the file (I don't know).

Tumelo
  • 301
  • 3
  • 18
  • 4
    If there was something that you as a programmer could do to get adminstrator rights without UAC, wouldn't the malware writers do that too? – slippyr4 Jul 17 '14 at 15:57
  • 2
    The whole point of UAC is to require privileges. If you don't want the user to be prompted, you'll need to tell them to turn off UAC. While you may think it's nonsense, it provides an effective way to handle privilege escalation and shouldn't normally be turned off. – mason Jul 17 '14 at 15:57
  • You might not be able to get around UAC, but you could right click on the icon of the app, go to properties > Shortcut > Advanced and select 'Run As Administrator'. It will at least ensure people are using it in the right mode and not blame you when they dont use it right :). Sorry it's a bit noddy, but it works. – JsAndDotNet Jul 17 '14 at 16:02
  • 1
    @Hockey that's what the manifest is for, see [How to force my .NET App to run as administrator on Windows 7?](http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7). – CodeCaster Jul 17 '14 at 16:07

2 Answers2

2

You will not get this working without UAC prompt. It's working as designed.

Dbl
  • 5,634
  • 3
  • 41
  • 66
  • Also if you have no malicious intend your chances on getting your users to install a WCF service should also solve your problem? – Dbl Jul 17 '14 at 16:02
  • thanks, I guess I'll have to go down that route . . . .#sigh – Tumelo Jul 17 '14 at 18:26
0

If you are willing to live with a console window appearing when this section of code appears for a brief moment you can have it give you a UAC prompt at the time this section of code runs instead of every time you start the program.

ProcessStartInfo procInfo = new ProcessStartInfo
{
    WorkingDirectory = Path.GetPathRoot(Environment.SystemDirectory),
    FileName = Path.Combine(Environment.SystemDirectory, "netsh.exe"),
    Arguments = "netsh http add urlacl url=http://" + GetIP() + ":80/VSPDataLogger/ user=everyone",
    UseShellExecute = true,
    startInfo.Verb = "runas";
};

Process proc = Process.Start(procInfo);
proc.WaitForExit();

There is no way to do it so no UAC prompt happens.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431