首页 新闻 会员 周边 捐助

asp.net core问题:"Parameter count mismatch between X-Forwarded-For and X-Forwarded-Proto"

0
悬赏园豆:20 [已解决问题] 解决于 2017-06-22 22:52

Startup 中下面的代码引起的:

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
dudu的主页 dudu | 高人七级 | 园豆:29839
提问于:2017-06-22 22:20
< >
分享
最佳答案
0

将 RequireHeaderSymmetry 设置为 false 可解决这个问题 

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
    RequireHeaderSymmetry = false                
});
dudu | 高人七级 |园豆:29839 | 2017-06-22 22:52

我设置了怎么都不起作用呢?

我的项目是 asp.net core 2.2,使用k8s+docker部署,ingress转发过来的,设置了UseForwardedHeaders之后,客户端浏览器使用https,但是Request.Scheme还是http,ingress使用Nginx转发过来都含有X-Forwarded-For和X-Forwarded-Proto

Alan_Liu | 园豆:61 (初学一级) | 2019-06-03 16:35

折腾了很久,最后在下面两个链接里找到了解决方案:
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);
Alan_Liu | 园豆:61 (初学一级) | 2019-06-03 16:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册