若要在 Windows 上通过传输层安全性 (TLS) 启用 HTTP/2,需要满足两个要求:
- 应用层协议协商 (ALPN) 支持,从 Windows 8.1 和 Windows Server 2012 R2 开始提供。
- 一组与 HTTP/2 兼容的密码,从 Windows 10 和 Windows Server 2016 开始可用。
因此,配置了 HTTP/2 over TLS 时 Kestrel 的行为已更改为:
- 当
Http1
设置为Information
时,降级到 并记录Http1AndHttp2
级别的消息。Http1AndHttp2
是ListenOptions.HttpProtocols
的默认值。 - 当
NotSupportedException
设置为ListenOptions.HttpProtocols
时,引发Http2
。
有关讨论,请参阅问题 dotnet/aspnetcore#23068。
已引入的版本
ASP.NET Core 5.0
旧行为
下表概述了通过 TLS 配置 HTTP/2 时的行为。
协议 | Windows 7、 Windows Server 2008 R2、 或更早版本 |
Windows 8、 Windows Server 2012 |
Windows 8.1、 Windows Server 2012 R2 |
Windows 10、 Windows Server 2016、 或更高版本 |
---|---|---|---|---|
Http2 |
引发 NotSupportedException |
TLS 握手期间出错 | TLS 握手期间出错 * | 无错误 |
Http1AndHttp2 |
降级到 Http1 |
降级到 Http1 |
TLS 握手期间出错 * | 无错误 |
* 配置兼容的密码套件以启用这些方案。
新行为
下表概述了通过 TLS 配置 HTTP/2 时的行为。
协议 | Windows 7、 Windows Server 2008 R2、 或更早版本 |
Windows 8、 Windows Server 2012 |
Windows 8.1、 Windows Server 2012 R2 |
Windows 10、 Windows Server 2016、 或更高版本 |
---|---|---|---|---|
Http2 |
引发 NotSupportedException |
引发 NotSupportedException |
引发 NotSupportedException ** |
无错误 |
Http1AndHttp2 |
降级到 Http1 |
降级到 Http1 |
降级到 Http1 ** |
无错误 |
** 配置兼容的密码套件,并将应用上下文开关 Microsoft.AspNetCore.Server.Kestrel.EnableWindows81Http2
设置为 true
启用这些方案。
更改原因
此更改可确保早期 Windows 版本上的 HTTP/2 与 TLS 的兼容性错误尽早浮出水面,并尽可能清晰地显示。
建议的措施
确保在不兼容的 Windows 版本上禁用基于 TLS 的 HTTP/2。 Windows 8.1 和 Windows Server 2012 R2 不兼容,因为它们默认缺少必要的密码。 但是,可以更新计算机配置设置以使用 HTTP/2 兼容的密码。 有关详细信息,请参阅 Windows 8.1 中的 TLS 密码套件。 配置完成后,必须通过设置应用上下文切换 Microsoft.AspNetCore.Server.Kestrel.EnableWindows81Http2
,在 Kestrel 上启用基于 TLS 的 HTTP/2。 例如:
AppContext.SetSwitch("Microsoft.AspNetCore.Server.Kestrel.EnableWindows81Http2", true);
未更改基础支持。 例如,基于 TLS 的 HTTP/2 从未在 Windows 8 或 Windows Server 2012 上运行。 此更改修改了这些不受支持的方案中的错误呈现方式。
受影响的 API
没有