Search code examples
asp.net-web-apilocalhostnetstat

ASP.NET Web API listens on 0.0.0.0 instead 127.0.0.1


My ASP.NET Web API is bound to localhost:

startOptions.Urls.Add("http://localhost:8080");

If I now call netstat -a I would have expected to see something like 127.0.0.1:8080 but it looks like my binding works on all IP addresses on the local machine:

Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    0.0.0.0:8080           MyComputer:0            LISTENING
  TCP    [::]:8888              MyComputer:0            LISTENING

Do I have to worry about external (e.g. non-localhost) connections?


Solution

  • "Yes", since this port may be routable by others you may need to worry about external connections, but it depends on your specific environment. Use the https://en.wikipedia.org/wiki/Localhost loop back address, 127.0.0.1 and then you won't have to have external access concerns:

    startOptions.Urls.Add("http://127.0.0.1:8080");
    

    Your question has to do with the default route, https://en.wikipedia.org/wiki/Default_route.

    FYI: You may want to have a configuration that switches this when you deploy so it can have access when you want it to...

    Additionally, https://ifconfig.co/port/8080 may be able to help you determine if your port is exposed...