Debug Azure Function locally with https on a custom domain

Description

If you want to deploy an Azure Function normally this endpoint is publicly available. Therefore you need to secure the connection to this endpoint at least with ssl. Before you deploy you also want to test the connection probably locally. The following steps should help you to setup a https connection to a local Azure Function.


1 STEP: Create a Self Signed Certificate via PowerShell

  • Open Windows Terminal or PowerShell as an Administrator
  • The following script will create a self signed certificate in the "Users" own certificate store with the password 12345
  • The script will also export the certificate as PFX to C:\server.pfx
  • The domain for the certificate will be "debug-local.com"


$cert = New-SelfSignedCertificate -DnsName "debug-local.com", "debug-local.com" -CertStoreLocation "cert:\LocalMachine\My"
$myPwd = ConvertTo-SecureString -String "12345" -Force -AsPlainText

Get-ChildItem -Path "cert:\localMachine\My\$($cert.Thumbprint)" | Export-PfxCertificate -FilePath C:\server.pfx -Password $mypwd


Certificate Store in Windows


2. STEP: Move to Trusted Certificates

  • In Windows search for "manage computer certificates" 
  • Move the certificate "debug-local.com" from "Own Certificates" to "Trusted Root Certificates". 


3. STEP: Edit hosts file

This will redirect the debug-local.com domain to your local machine:
  • Open Notepad as an Administrator
  • Goto: C:\Windows\System32\drivers\etc\hosts
  • Add following line
    • 127.0.0.1       debug-local.com
  • 127.0.0.1 is the address of your local machine


4. STEP: Move certificate to your Azure Function folder

If you are using Visual Studio:

  • Copy the server.pfx file to the root folder of your Azure Function project
  • Right click on the file
  • Select Copy if newer

5. STEP: Configure Azure Function to start with https

Before Debug in Visual Studio (I use 2022)
  • Right click on Azure Function project
  • Select Options
  • Debug => General
  • Open Debug Profiles
  • Paste following line into the textbox under "Command Line Arguments" section
    • host start --useHttps --cert server.pfx --password 12345 --cors *
    • this should create a launchSettings.json in the properties folder of the project

      Visual Studio 2022 - Debug Settings


  • Right click on the Azure Function project
  • Debug => Start New Instance
  • A new Azure Function instance should start with https://localhost:port
  • If you Azure Function has an endpoint "GetData" you can now call
    • https://debug-local.com/api/getdata

Before Debug in Visual Studio Code
  • Open Tasks.json
  • Modify the "Run Function Host" task

6. STEP: Possible Problems

If you see the "ERR_CERT_AUTHORITY_INVALID" error it can be that you forgot to move to the certificate to "Trusted Root Certificates" store.
  • Move the certificate like in step 2
  • Browser cache normally certificates. You should clear the certificate cache
    • Goto Internet Options => Content
    • Clear SSL-Status

    • Clear cached certificates


If you see the error "Wrong Network Password" you have probably mistyped the certificate password in the debug configuration.

Comments

Popular posts from this blog

What is Base, Local & Remote in Git merge

Asynchronous Nunjucks with Filters and Extensions