Startup 中下面的代码引起的:
app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });
将 RequireHeaderSymmetry 设置为 false 可解决这个问题
app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto, RequireHeaderSymmetry = false });
我设置了怎么都不起作用呢?
我的项目是 asp.net core 2.2,使用k8s+docker部署,ingress转发过来的,设置了UseForwardedHeaders之后,客户端浏览器使用https,但是Request.Scheme还是http,ingress使用Nginx转发过来都含有X-Forwarded-For和X-Forwarded-Proto
折腾了很久,最后在下面两个链接里找到了解决方案:
https://github.com/aspnet/AspNetCore.Docs/issues/2384
https://stackoverflow.com/questions/43749236/net-core-x-forwarded-proto-not-working/44390593
需要设置KnownNetworks和KnownProxies才起作用
var options = new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
};
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
app.UseForwardedHeaders(options);