How to enable azure function https easily when do local test

Viewed 4916

I want to test Azure Function Http trigger locally on windows.

I use azure-function-core-tools to run command like func start --port 5007 --useHttps

And then I got error : Auto cert generation is currently not working on the .NET Core build.

It seems like that I should use command like func start --port 5007 --useHttps --cert certificate.pfx after I creating a self-signed certificate locally.

If there is a way to enable https easily?

The way like when I use .net core webapi, I easily export env:ASPNETCORE_URLS=https://localhost. And after clicking some button, I can use https well. Do we have some thing like this?

Or if func start --port 5007 --useHttps is enough but i miss some configuration?

Thank you!

3 Answers

Update Answer:

func start --port 5007 --useHttps will not automatic create certificate after azure function v1. azure function v2 and azure function v3 tools which is based on .Net Core will not create certificate for you. You should generate the certificate manully.

Original Answer:

I can reproduce your error:

enter image description here

Solution:

Use administrator to start your powershell, go to your function app folder, and then use this command:

$cert = New-SelfSignedCertificate -Subject localhost -DnsName localhost -FriendlyName "Functions Development" -KeyUsage DigitalSignature -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.1")

And then create the certificate file:

Export-PfxCertificate -Cert $cert -FilePath certificate.pfx -Password (ConvertTo-SecureString -String 123 -Force -AsPlainText)

At last, I can use https:(below command runs in windows cmd.)

func host start --port 5007 --useHttps --cors * --cert certificate.pfx --password 123

enter image description here

As an addition to Bowman Zhu's answer, you can also configure VS to then use the certificate when you run the project.

Go to the project properties and select the Debug tab. In the Application arguments, add the following:

host start --useHttps --cert "certificate.pfx" --password "123"

You can also include the certificate within your project and set it to Copy if newer to deploy it to your bin.

Azure Function Core Tools (V2) now has a new switch so that you don't have to use the --cert and --password switches any more. You can now use:

host start --useHttps --useDefaultCert

Additionally, for completeness, you can create your local self-signed certificate for dotnet core using:

dotnet dev-certs https --trust
Related