Web API self host - bind on all network interfaces

前端 未结 2 1385
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 03:48

How do you make a Web API self host bind on all network interfaces?

I have the below code currently. Unfortunately, it binds only on localhost. So access to this se

相关标签:
2条回答
  • 2020-12-29 03:52

    If you get access exceptions, please DO NOT start Visual Studio as admin user. Add an URL reservation instead. The following example assumes that you want to open port 9000 as HTTP service on all ports & hostnames (http://+:9000/) without any user restriction.

    Start a command console window as administrator and execute:

    netsh
    netsh> http add urlacl url="http://+:9000/" sddl=D:(A;;GX;;;S-1-1-0)
    

    The SDDL translates to "all users" from your current domain / machine.

    Modify your code accordingly:

    var baseAddress = "http://+:9000/";
    using (WebApp.Start<Startup> (baseAddress)) 
    {
      // your code here
    }
    

    You can delete the reservation by running:

    netsh
    netsh> http delete urlacl url="http://+:9000/"
    

    However, Microsoft recommends to avoid Top-level wildcard bindings, see:

    • https://docs.microsoft.com/en-us/windows/win32/http/add-urlacl

    For more information about the difference between http://*:9000/ and http://+:9000/ see:

    • What does a plus sign mean in a http url? -> http://+:80
    0 讨论(0)
  • 2020-12-29 04:07

    Just change the base address like this

            var baseAddress = "http://*:9000/"; 
            using (WebApp.Start<Startup> (baseAddress)) 
            {
                Console.WriteLine("Server started");
                Thread.Sleep(1000000);
            }
    

    And it should bind correctlly to all interfaces.

    0 讨论(0)
提交回复
热议问题