Skip to content

Instantly share code, notes, and snippets.

@Fireforge
Created April 12, 2016 21:07
Show Gist options
  • Save Fireforge/e5f577d645b17243489a567e4b77e429 to your computer and use it in GitHub Desktop.
Save Fireforge/e5f577d645b17243489a567e4b77e429 to your computer and use it in GitHub Desktop.
Using netsh to add the URL used by HTTPListener
/// <summary>
/// Reserve the URL we'll be using for this session for a given user on a given domain
/// Source: http://stackoverflow.com/questions/14962334/httplistenerexception-access-denied-for-non-admins on Oct 09, 2014
/// </summary>
/// <param name="address">URL (something like http://<ip_address>:<port>/)</param>
/// <param name="domain">Domain name (the current one is from Environment.UserDomainName)</param>
/// <param name="user">User name (the current one is from Environment.UserName)</param>
public static void AddAddress(this HttpListener listener, string address, string domain, string user)
{
string args = string.Format(@"http add urlacl url={0}", address) + " user=\"" + domain + "\\" + user + "\"";
ProcessStartInfo psi = new ProcessStartInfo("netsh", args);
psi.Verb = "runas";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
Process.Start(psi).WaitForExit();
listener.Prefixes.Add(address);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment