Search code examples
httplocalhost.net-6.0iis-expressasp.net-4.8

Unable to replace localhost with 127.0.0.1 in URL


I have two local apps running:

  • ASP.NET client (ASP.NET 4.8): https://localhost:44300
  • Duende Identity Server (.NET 6): https://localhost:44349

The Identity Server cookies are being sent to the client application when the browser makes request to the client application.

Since this wouldn't happen in prod due to them being two different URLs, I tried this answer which says to use one as 127.0.0.1, and the other as localhost, in order to replicate how the cookies would be sent in prod.

However, when I try to access https://127.0.0.1:44300, I get Bad Request - Invalid Hostname.

I've checked C:\Windows\System32\drivers\etc\hosts and can see it has these two lines:

127.0.0.1       localhost
::1             localhost

I've also tried the suggestions in this answer, but it didn't work.

How can I access localhost using 127.0.0.1?


Solution

  • My goal was to replace localhost with 127.0.0.1 in the URL. Originally I was getting Bad Request - Invalid Hostname

    This worked for me (Microsoft Visual Studio Professional 2022 (64-bit) - Current - Version 17.6.4)

    Run cmd as admin and run these commands:

    netsh http add urlacl url="https://*:portnumber/" user=everyone
    netsh http add urlacl url="https://localhost:portnumber/" user=everyone
    

    Make a backup of \.vs\config\applicationhost.config.

    Edit \.vs\config\applicationhost.config to have this in your app's bindings section:

    <binding protocol="https" bindingInformation="*:44300:localhost" />
    <binding protocol="https" bindingInformation="*:44300:*" />
    

    Make sure it's listed in that order.

    Run VS as admin, and you should be able to go to https://127.0.0.1:portnumber.

    I've written about this here.