|
| 1 | +--- |
| 2 | +title: "Develop Your ASP.NET .NET And Nuxt Web Application using HTTPS" |
| 3 | +lead: Use HTTPS in local development environment |
| 4 | +date: 2025-02-28 |
| 5 | +image: |
| 6 | + src: /images/dotnet_nuxt_https.webp |
| 7 | +badge: |
| 8 | + label: Development |
| 9 | +tags: |
| 10 | + - ASP.NET Core |
| 11 | + - Nuxt |
| 12 | + - Vue.js |
| 13 | + - .NET |
| 14 | + - HTTP |
| 15 | + - Security |
| 16 | +--- |
| 17 | + |
| 18 | +In my [last article](https://techwatching.dev/posts/aspnetcore-with-nuxt), we explored how to integrate an ASP.NET Core API with a Nuxt.js front end. However, we were using HTTP to debug both the API and the front end locally. |
| 19 | + |
| 20 | +There are a lot of reasons why we would want to use HTTPS instead of HTTP for local development, but mainly it's about having a local setup that closely matches the production environment. Our web application will of course use HTTPS when deployed to production and other environments, so it's better to identify any potential issues related to HTTPS as early as possible. |
| 21 | + |
| 22 | +So let’s switch to HTTPS for the ASP.NET Core and Nuxt.js [application we previously created](https://github.com/TechWatching/AspnetWithNuxt/tree/initial-without-aspire). |
| 23 | + |
| 24 | +## Use HTTPS for the ASP.NET Core API |
| 25 | + |
| 26 | +To make HTTPS work, we need to generate a self-signed certificate and trust it on our local environment. There are [different ways to do that](https://learn.microsoft.com/en-us/dotnet/core/additional-tools/self-signed-certificates-guide?wt.mc_id=MVP_430820), but to keep things simple we can use the [built-in command of the .NET CLI](https://learn.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-9.0&tabs=visual-studio%2Clinux-sles&wt.mc_id=MVP_430820#trust-the-aspnet-core-https-development-certificate) `dev-certs` that will install an ASP.NET Core HTTPS Development certificate: |
| 27 | + |
| 28 | +```bash |
| 29 | +dotnet dev-certs https --trust |
| 30 | +``` |
| 31 | + |
| 32 | +In the `launchsettings.json` file of the `WebAPI` project, you can see there are 2 profiles: |
| 33 | + |
| 34 | +```json [launchsettings.json] |
| 35 | +{ |
| 36 | + "$schema": "https://json.schemastore.org/launchsettings.json", |
| 37 | + "profiles": { |
| 38 | + "http": { |
| 39 | + "commandName": "Project", |
| 40 | + "dotnetRunMessages": true, |
| 41 | + "launchBrowser": false, |
| 42 | + "applicationUrl": "http://localhost:5096", |
| 43 | + "environmentVariables": { |
| 44 | + "ASPNETCORE_ENVIRONMENT": "Development" |
| 45 | + } |
| 46 | + }, |
| 47 | + "https": { |
| 48 | + "commandName": "Project", |
| 49 | + "dotnetRunMessages": true, |
| 50 | + "launchBrowser": false, |
| 51 | + "applicationUrl": "https://localhost:7238;http://localhost:5096", |
| 52 | + "environmentVariables": { |
| 53 | + "ASPNETCORE_ENVIRONMENT": "Development" |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +By default, the first profile is used when you run the project without specifying a profile: |
| 61 | + |
| 62 | +```bash |
| 63 | +dotnet run --project WebApi\WebApi.csproj |
| 64 | +``` |
| 65 | + |
| 66 | +To use the `https` profile, we can run this command: |
| 67 | + |
| 68 | +```bash |
| 69 | +dotnet run --project WebApi\WebApi.csproj -lp https |
| 70 | +``` |
| 71 | + |
| 72 | +And now the `WebApi` runs in HTTPS locally: |
| 73 | + |
| 74 | +{.rounded-lg.mx-auto width="400"} |
| 75 | + |
| 76 | +## Configure the Nuxt front end to work with the HTTPS API endpoint |
| 77 | + |
| 78 | +Because we changed the `WebApi` to HTTPS, the endpoint has changed, so we have to change the URL for the API on the `WebApp`. |
| 79 | + |
| 80 | +We can do that in the `nuxt.config.ts`: |
| 81 | + |
| 82 | +```typescript [nuxt.config.ts] |
| 83 | +routeRules: { |
| 84 | + '/api/**': { |
| 85 | + proxy: 'https://localhost:7238/**', |
| 86 | + } |
| 87 | +}, |
| 88 | +``` |
| 89 | + |
| 90 | +As [indicated in Nuxt documentation](https://nuxt.com/docs/api/composables/use-fetch), “to call an (external) HTTPS URL with a self-signed certificate in development, **you will need to set** `NODE_TLS_REJECT_UNAUTHORIZED=0` **in your environment.” We can set this environment variable directly in the** `dev` script in `package.json` (as it is explained in this [article](https://www.scriptedpixels.co.uk/blog/using-https-with-nuxt3)) but the syntax would differ depending on the shell used. So instead we will set it directly in an `.env` file: |
| 91 | + |
| 92 | +```plaintext [.env] |
| 93 | +NODE_TLS_REJECT_UNAUTHORIZED=0 |
| 94 | +``` |
| 95 | + |
| 96 | +Now, the `WebApp` is working again, this time by using the `WebApi` HTTPS endpoint. |
| 97 | + |
| 98 | +{.rounded-lg.mx-auto width="400"} |
| 99 | + |
| 100 | +Yet, we are still using HTTP for the front itself, let’s change that. |
| 101 | + |
| 102 | +## Use HTTPS for the Nuxt front end |
| 103 | + |
| 104 | +In the Nuxt configuration, there is an `https` section with `key` and `cert` properties to specify the private key and certificate to use. We already have an ASP.NET Core HTTPS Development certificate, so we can use it for the `WebApp` by exporting the necessary files with the `dotnet dev-certs` command: |
| 105 | + |
| 106 | +```bash |
| 107 | +dotnet dev-certs https --export-path ./WebApp/dev-cert.pem --format PEM -np |
| 108 | +``` |
| 109 | + |
| 110 | +This will generate a `dev-cert.pem` file and a `dev-cert.key` file. Make sure to configure properly your `.gitignore` to avoid committing these files. |
| 111 | + |
| 112 | +::callout{icon="i-heroicons-light-bulb"} |
| 113 | +If you have played with the Visual Studio templates for `Vue`, `React` or `Angular`, generating a certificate using `dotnet dev-certs` and exporting the corresponding files is exactly what the code of the `vite.config.ts` file in the templates does. |
| 114 | +:: |
| 115 | + |
| 116 | +Then, we can modify the `nuxt.config.ts` file: |
| 117 | + |
| 118 | +```typescript [nuxt.config.ts] |
| 119 | + $development: { |
| 120 | + routeRules: { |
| 121 | + '/api/**': { |
| 122 | + proxy: 'https://localhost:7238/**', |
| 123 | + } |
| 124 | + }, |
| 125 | + devServer: { |
| 126 | + https: { |
| 127 | + key: 'dev-cert.key', |
| 128 | + cert: 'dev-cert.pem' |
| 129 | + } |
| 130 | + } |
| 131 | + }, |
| 132 | +``` |
| 133 | + |
| 134 | +And here is the result, both the front end and the API using HTTPS: |
| 135 | + |
| 136 | +{.rounded-lg.mx-auto width="400"} |
| 137 | + |
| 138 | +To create a production-like environment during local development, it's a good idea to use HTTPS. Using tools like the .NET CLI to generate and trust self-signed certificates makes it easier to set up HTTPS for both the ASP.NET Core API and the Nuxt.js front end. |
| 139 | + |
| 140 | +The code for this article is available in this [GitHub repository](https://github.com/TechWatching/AspnetWithNuxt/tree/1a5dc26c18daf0e24d99de3763fdb407e807b4b1). |
0 commit comments