1

I'm using ASP.NET Core. There are questions/answers on Stackoverflow related to developing with custom URLs already, but my situation seems to be a little different, as I need both https support and a custom base URL. I'm authorizing with a 3rd party with OAuth redirect flow. The 3rd party only supports redirect URLs that are https but not localhost. Right out of the box, Visual Studio 2017 (version 15.7.2) configured https support, which was a nice surprise. By default, my app launches on https://localhost:44348/.

launchSettings.json looks like this:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:53837",
      "sslPort": 44348
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyProject.Web": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

I've added a host entry to map a custom url to 127.0.0.1

127.0.0.1       app.mysite.local

...but I'm not able to access my site at https://app.mysite.local:44348/. I get a Bad Request - Invalid Hostname error.

I've tried changing this entry in .vs/config/applicationhost.config from:

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

to

<binding protocol="https" bindingInformation="*:44348:*" />

but then my app won't run at all, even if I run Visual Studio as an Administrator. Visual Studio shows this dialog:

enter image description here

I've even tried using the UseUrls() extension method on IWebHostBuilder:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseUrls("https://*:44348/");
}

...but none of those techniques seem to work. Can anyone shed some light on how to get a custom base URL to work on https when developing ASP.NET Core apps?

Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
  • Could you provide more from your launchsettings, im guessing you also have profiles in there. Also guessing it shouldnt be an issue but why are you not using app.mysite.local instead of localhost in the applicationUrl? Have you restarted since you made the update to the hosts file. I remember that being a potential issue. – eVolve Mar 30 '19 at 17:05
  • @eVolve I updated my post with the full `launchSettings.json`. Yes, there are profiles there. I tried changing the applicationUrl to app.mysite.local, but I got the same `Invalid URI: The hostname could not be parsed` error message as the screenshot above. I don't think it's the host file not updating, as the URL seems to be routing to the application correctly. – Johnny Oshika Mar 31 '19 at 07:24

1 Answers1

1

Have have this running with the updated url but its missing the cert which is probably the next thing to look at. Granted I know this is not the complete answer but I thought it better to provide some help as I don't currently have time to try getting passed the cert issue.

Try changing .vs/config/applicationhost.config to the following (you will need to update the port to the one you are using) so looking at the bindings notice in my config I updated it in two places:

      <site name="WebSite1" id="1" serverAutoStart="true">
    <application path="/">
      <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />
    </application>
    <bindings>
      <binding protocol="http" bindingInformation=":8080:localhost" />
      <binding protocol="https" bindingInformation="*:44335:app.mysite.local" />
    </bindings>
  </site>
  <site name="WebApplication2" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
      <virtualDirectory path="/" physicalPath="C:\Users\benl\source\repos\WebApplication2\WebApplication2" />
    </application>
    <bindings>
      <binding protocol="http" bindingInformation="*:63297:localhost" />
      <binding protocol="https" bindingInformation="*:44335:app.mysite.local" />
    </bindings>
  </site>

Additionally I was running VS as an admin. I did experience and IIS error so I did do a restart. App loads on the localhost url and complains about the bad request. So now enter in your url https://app.mysite.local:44335/ (again port change) and it now is running:

enter image description here

The next part you will need to sort a new cert for IIS express as its most likely only been setup for localhost. Link to possibly how to do this

Also this may be of use Link to cert creation / use for localdev

eVolve
  • 1,340
  • 1
  • 11
  • 30
  • Brilliant, it works! It looks like you added the `app.mysite.local` binding in `WebSite1`, but I don't think it's necessary. Only the one in `WebApplication2` seems to be necessary. If you leave the old https localhost binding in there in addition to the new https app.mysite.local binding, you will be able to serve the site on both `https://localhost:44335` and `https://app.mysite.local:44335`. Sadly, the moment you add the `app.mysite.local` binding, you need to run Visual Studio as an administrator or IIS won't start. – Johnny Oshika Apr 05 '19 at 04:43