I'm working on a project in .NET Framework 4.6.1, C# and WinForms (offline desktop app). I come from web-dev and I'm not very familiar with desktop apps.
For part of the app, I am using HttpSelfHostServer
to give me API functionality.
var config = new HttpSelfHostConfiguration("http://localhost:8080/api/");
config.Routes.MapHttpRoute(
"default", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
config.EnableCors(new EnableCorsAttribute(origins: "*", headers: "*", methods: "*"));
// pass in config to new server instance
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
This throws an error: AddressAccessDeniedException: HTTP could not register URL http://+:8080/api/. Your process does not have access rights to this namespace
I have tried:
Use the
netsh http add urlacl url=http://+:8080/api/ user=everyone
command from the CMD as Administrator which fixes the issue but this is a bad solution. The end-user of the app should not have to use the command line.Programatically run the above command:
var netshProcess = new Process { StartInfo = { FileName = "cmd", Arguments = "netsh http add urlacl url=http://+:8080/api/ user=everyone", UseShellExecute = true, Verb = "runas", //WindowStyle = ProcessWindowStyle.Hidden } }; try { netshProcess.Start(); } catch (Exception portRegisterError) { Console.WriteLine(portRegisterError); throw; }
According to similar issues that I have found this should execute the command with Admin rights but all it does in my case is: opens Windows User Acces Control to confirm, then opens the command prompt window with no commands passed in (resulting in throwing the same AddressAccessDeniedException
).
What am I missing?
I know that all this may seem like a 'hacky' way to do things but for various reasons, I do not have much choice in this and I just have to make this work one way or another without asking the end-user to do stuff.
I'd apriaciate any advice.
Many thanks.