5

I want to be able to develop locally using a custom domain and ssl rather than localhost.

How can I setup a custom domain + ssl in VS Solution instead of localhost?

user2818430
  • 5,853
  • 21
  • 82
  • 148

4 Answers4

7

Simple Setup - Using Server URLs

If you want to associate your server to use all the IP addresses assigned to the server/web host then you can do this:

var host = new WebHostBuilder()
    .UseUrls("http://*:80", "http://localhost")
    .UseKestrel()
    .UseIISIntegration()
    .Build();

Note: If you don't want all IP addresses, then you can change from http://* to a specific IP address such as http://111.111.111.111. Also, the port is not a requirement, but I have used it for completeness of the answer. It's also important to note that SSL won't work with UseUrls

There is a great amount of additional detail that you can find over at the official Microsoft Docs about Server URLs here.


Binding SSL Certifications (Kestrel Only) -- Endpoint Configuration

Please note that hosting over a public endpoint via Kestrel (even with SSL) is not recommended and you should use a reverse proxy like Nginx or IIS. You can read more about it from the official Microsoft Docs here.

You didn't mention if you were using Kestrel or not, but I will assume you are... In which case, you can configure an SSL certificate easily by binding sockets using the options.

Here is an example of using TCP sockets using the Listen method:

public static void Main(string[] args)
{
    BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseKestrel(options =>
        {
            options.Listen(IPAddress.Loopback, 5000);
            options.Listen(IPAddress.Loopback, 5001, listenOptions =>
            {
                listenOptions.UseHttps("testCert.pfx", "testPassword");
            });
        })
        .UseIISIntegration() // <-- don't forget you will need this for IIS!
        .Build();

Note: That if you use both the Listen method and UseUrls, the Listen endpoints override the UseUrls endpoints.

You can find more info here at the official Microsoft Docs.


Configuring IISExpress

Using the GUI
You can right-click the project and click [Properties].

enter image description here

Using launchSettings.json.
You have to configure this using the launchSettings.json which you can find here:

launchSettings.json

"iisSettings": {
  "windowsAuthentication": false,
  "anonymousAuthentication": true,
  "iisExpress": {
    "applicationUrl": "http://localhost:61471/",
    "sslPort": 44360
  }
},
"profiles": {
  "IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "launchUrl": "https://localhost:44360",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

Configuring IIS Endpoints

If you use IIS, the URL bindings for IIS override any bindings that you set by calling either Listen or UseUrls. For more information, see Introduction to ASP.NET Core Module.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
Svek
  • 12,350
  • 6
  • 38
  • 69
  • ooh, haven't seen that before, will have a look. – Mark Redman Oct 07 '17 at 09:53
  • 1
    Hosting Kestrel SSL for development is pretty much okay. Kestrel only shouldn't be used as internet facing server (IFS), since that's what the quote from above is about. Also please note, that the launchSettings.json shouldn't be edited manually. Use the project properties -> Debug UI for that. Editing the launchsettings.json can result that the settings are not applied to IISExpress configs (in .vs/config/applicationhost.config file) – Tseng Oct 07 '17 at 13:43
  • @Tseng - I didn't know about not editing the `launchSettings.json` is that a bug? Regardless, always great to get your input. – Svek Oct 07 '17 at 14:08
  • I wouldn't consider it a bug. The file is typically created by that UI and it keeps IIS setttings in sync with `.vs/config/applicationhost.config`. Only changing the port in `launchsettings.json` (at least in the past) resulted in only changed startup url, but the application still running on old port, because the `applicationhost.config` wasn't updated. Doing it via UI would update both files in the past. – Tseng Oct 07 '17 at 14:11
  • 1
    SUPER IMPORTANT: Your port for SSL in IISExpress must be in the range 44300-44399. Your original answer had 443 shown for the sslPort, but your image showed a port of 44360. Oddly you can't edit this port in the properties page as far as I can see. Anyway I edited your answer to match the port in your image in the launchsettings.json (from 443 -> 44360) – Simon_Weaver Dec 10 '18 at 05:16
2

For .net core, to setup a custom domain:

  1. Add domain to the hosts file, something like www.yourapp.local

  2. find the solution /.vs/applicationhost.config Add binding e.g.:

  3. In the web project properties > Debug add the App Url to "http://www.yourapp.local:51791/"

(replace port number as required)

For SSL, I assume you can set the above bindings and settings to https and in the web app properties > Debug tick the "Enable SSL" checkbox.

also see this answer: How to enable SSL for IIS Express in VS2015

Peter B
  • 22,460
  • 5
  • 32
  • 69
Mark Redman
  • 24,079
  • 20
  • 92
  • 147
1

If you're fine with the certificate validation error in your browser (or whatever client you use), you can put an entry in your hosts file:

www.yourcustomdomain.com 127.0.0.1 (or ::1 if you're on IPv6)

and then use the custom domain to reach your web site locally.

In Windows the hosts file is usually located at C:\Windows\System32\drivers\etc.

MEMark
  • 1,493
  • 2
  • 22
  • 32
0

First, add an entry in the client's C:\Windows\System32\drivers\etc\hosts text file. Follow instructions in the hosts file itself.

By "develop locally" do you mean on the local machine or local network? If the latter, you must complete the following tasks (in any order).

  • Generate as described here and configure as described here a certificate on the server, and install it on the client.
  • Configure the firewall to allow access to your web API as described here.
  • Bind your web API to a non-localhost URL as described here and here.

I'm not sure off hand, but to get it working with IIS Express you might also need to run netsh http add urlacl as described here and here.

Some of the above links are specific to IIS Express since that's what you asked about. If using Kestrel, then vary the above tasks as follows.

To configure your certificate on the server, add this to appsettings.json:

"Kestrel": {
    "Certificates": {
        "Default": {
            "Subject": "api.mycustomdomain.com",
            "Store": "My",
            "AllowInvalid": true
        }
    }
}

To bind your web API to a non-localhost URL, in launchSettings.json's Kestrel profile, replace the localhost part of applicationUrl's value with 0.0.0.0.

HappyNomad
  • 4,458
  • 4
  • 36
  • 55