Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 2 - develop using custom domain names and ssl using IISExpress

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?

like image 368
user2818430 Avatar asked Oct 07 '17 08:10

user2818430


People also ask

What is the use of UseHttpsRedirection?

HTTPS Redirection Middleware (UseHttpsRedirection) to redirect HTTP requests to HTTPS. HSTS Middleware (UseHsts) to send HTTP Strict Transport Security Protocol (HSTS) headers to clients.

What is Hsts in asp net core?

Posted on: 22-01-2017 6 Comments. This is a continuation to the previous article on Enforcing HTTPS. While redirecting all non-secure requests to secure URLs is good, a man-in-the-middle can still hijack the connection before the redirect.

Does .NET core use IIS?

IIS and ASP.NET Core The most important thing to understand about hosting ASP.NET Core is that it runs as a standalone, out of process Console application. It's not hosted inside of IIS and it doesn't need IIS to run.


3 Answers

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.

like image 146
Svek Avatar answered Sep 28 '22 04:09

Svek


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

like image 40
Mark Redman Avatar answered Sep 28 '22 04:09

Mark Redman


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.

like image 38
MEMark Avatar answered Sep 28 '22 02:09

MEMark