Exception thrown: The remote certificate is invalid according to the validation procedure
When you run the code using VS 2019, it installs a self-signed certificate on your machine for https redirection. (a message box is displayed informing the certificate installation).
But VS code doesn't do it for you.
Read this doc should help : https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-3.0&tabs=visual-studio#trust-the-aspnet-core-https-development-certificate-on-windows-and-macos
You can try to run this commands
dotnet dev-certs https --clean
dotnet dev-certs https --trust