CloudFlare
IIS ARR
Asp.net core 1.1.1
我落后于cloudflare,然后落后于iis。
cloudflare处于仅http模式
我一直在
X-Forwarded-For和。之间的参数计数不匹配 X - 转发,原
我的启动文件
ForwardedHeadersOptions forwardOption = new ForwardedHeadersOptions
{
ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor | Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto,
ForwardLimit = 2
};
forwardOption.KnownProxies.Add(IPAddress.Parse("xxxxx"));
forwardOption.KnownProxies.Add(IPAddress.Parse("xxxxx"));
forwardOption.KnownProxies.Add(IPAddress.Parse("xxxxxx"));
forwardOption.KnownProxies.Add(IPAddress.Parse("xxxxxx"));
app.UseForwardedHeaders(forwardOption);
我在iis arr级别设置了这个重写规则我也测试了https
HTTP_X_FORWARDED_PROTO
http
replace the existing value
问题是什么?
这是一个link,但没有帮助
答案 0 :(得分:1)
我找到了一个解决方案,只需在configureservices上添加此代码
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.RequireHeaderSymmetry = false;
options.ForwardLimit = 2;
options.KnownProxies.Add(IPAddress.Parse("xxx.xxx.xxx.xxx"));
options.KnownProxies.Add(IPAddress.Parse("xxx.xxx.xxx.xxx"));
options.KnownProxies.Add(IPAddress.Parse("xxx.xxx.xxx.xxx"));
});
并且你的basecontroller或其他地方使用此代码获取真正的ip地址,如果你落后于cloudflare和arr
public string GetRealIpAddress()
{
StringValues connectionIp;
var conncetionIpAvailable = Request.Headers.TryGetValue("Cf-Connecting-Ip", out connectionIp);
if (conncetionIpAvailable)
{
return connectionIp.ToString().Trim();
}
else
{
var xForwardFor = Request.Headers.GetCommaSeparatedValues("X-Forwarded-For");
string ipList = "";
foreach (var x in xForwardFor)
{
ipList = ipList.Trim() + " , " + x.Trim();
}
if (!string.IsNullOrEmpty(ipList.Trim()))
{
return ipList.Trim();
}
else
{
return "xxx.xxx.xxx.xxx";
}
}
}