Windows 11本地运行Azure Functions遇TLS握手失败问题求助
升级至Windows 11后,无法在VSCode本地运行Azure Functions API,出现Handshake Failure错误,具体错误日志如下:
[2023-10-19T10:23:15.097Z] Error building configuration in an external startup class.
[2023-10-19T10:23:15.104Z] Error building configuration in an external startup class. System.Net.Http: The SSL connection could not be established, see inner exception. System.Net.Security: Authentication failed because the remote party sent a TLS alert: 'HandshakeFailure'. The message received was unexpected or badly formatted.
[2023-10-19T10:23:15.263Z] A host error has occurred during startup operation '30896d42-9414-42f4-a910-c6e212cddec7'.
[2023-10-19T10:23:15.268Z] Microsoft.Azure.WebJobs.Script: Error building configuration in an external startup class. System.Net.Http: The SSL connection could not be established, see inner exception. System.Net.Security: Authentication failed because the remote party sent a TLS alert: 'HandshakeFailure'. The message received was unexpected or badly formatted.
Value cannot be null. (Parameter 'provider')
经排查:
- Windows 11默认启用TLS 1.3
- 本地调试时Azure Functions模拟器会连接CDN服务,该服务仅支持TLS 1.2
- 当前核心配置:
"AzureWebJobsStorage": "UseDevelopmentStorage=true" - 运行Fiddler可临时解决(流量通过Fiddler隧道,Fiddler使用TLS 1.2),但通过PowerShell执行
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;无效果
需求:强制VSCode或Azure Functions模拟器使用TLS 1.2解决该问题
以下几种方法可强制指定TLS 1.2,解决握手失败问题:
1. 修改系统注册表强制优先使用TLS 1.2
通过注册表设置让系统优先采用TLS 1.2,步骤如下:
- 按下
Win + R,输入regedit打开注册表编辑器 - 导航到路径:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp - 新建DWORD(32位)值,命名为
DefaultSecureProtocols,设置值为0x00000A00(对应TLS 1.2) - 再导航到
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp,重复上述步骤添加相同键值 - 重启电脑生效
2. 配置VSCode启动参数指定TLS版本
修改VSCode的启动配置,强制其运行时使用TLS 1.2:
- 右键VSCode快捷方式,选择「属性」
- 在「目标」栏末尾添加参数:
--ssl-protocol=TLSv1.2
示例:"C:\Users\<你的用户名>\AppData\Local\Programs\Microsoft VS Code\Code.exe" --ssl-protocol=TLSv1.2 - 点击「应用」「确定」,重启VSCode后尝试调试
3. 为Azure Functions设置系统环境变量
通过环境变量强制Azure Functions宿主使用TLS 1.2:
- 按下
Win + R,输入sysdm.cpl打开系统属性 - 切换到「高级」标签,点击「环境变量」
- 在「系统变量」区域点击「新建」:
- 变量名:
DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER,变量值:0 - 再新建变量:变量名
SystemDefaultTlsVersions,变量值:1
- 变量名:
- 保存设置后,重启VSCode和Azure Functions模拟器
4. 修改Azure Functions本地配置文件
在项目的local.settings.json中添加配置,强制禁用TLS 1.3:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet", "WEBSITE_LOAD_CERTIFICATES": "*" }, "Host": { "LocalHttpPort": 7071, "CORS": "*", "CORSCredentials": false }, "EnvironmentVariables": { "DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER": "0", "SystemDefaultTlsVersions": "1" } }
保存后重启调试会话
内容的提问来源于stack exchange,提问作者Sturdy

