您能否让我知道在使用MVC 6 Request.ServerVariables["REMOTE_ADDR"]
无法正常工作时如何在ASP.NET中获取客户端IP地址。
您能否让我知道在使用MVC 6 Request.ServerVariables["REMOTE_ADDR"]
无法正常工作时如何在ASP.NET中获取客户端IP地址。
Answers:
API已更新。不确定何时更改,但是据达米安·爱德华兹(Damien Edwards)所说,您现在可以执行以下操作:
var remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;
RemoteIpAddress
始终null
是我的选择。
在project.json中将依赖项添加到:
"Microsoft.AspNetCore.HttpOverrides": "1.0.0"
在Startup.cs
中的Configure()
方法中,添加:
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto
});
而且当然:
using Microsoft.AspNetCore.HttpOverrides;
然后,我可以使用以下命令获取ip:
Request.HttpContext.Connection.RemoteIpAddress
就我而言,在VS中进行调试时,我总是拥有IpV6本地主机,但是在IIS上部署时,我总是具有远程IP。
一些有用的链接: 如何在ASP.NET CORE中获取客户端IP地址?并且RemoteIpAddress始终为null
该::1
也许是因为:
IIS处的连接终止,然后将其转发到v.next Web服务器Kestrel,因此与Web服务器的连接确实来自本地主机。(https://stackoverflow.com/a/35442401/5326387)
可以添加一些后备逻辑来处理负载均衡器的存在。
另外,通过检查,X-Forwarded-For
即使没有负载均衡器,报头也可能会被设置(可能是由于附加的Kestrel层?):
public string GetRequestIP(bool tryUseXForwardHeader = true)
{
string ip = null;
// todo support new "Forwarded" header (2014) https://en.wikipedia.org/wiki/X-Forwarded-For
// X-Forwarded-For (csv list): Using the First entry in the list seems to work
// for 99% of cases however it has been suggested that a better (although tedious)
// approach might be to read each IP from right to left and use the first public IP.
// http://stackoverflow.com/a/43554000/538763
//
if (tryUseXForwardHeader)
ip = GetHeaderValueAs<string>("X-Forwarded-For").SplitCsv().FirstOrDefault();
// RemoteIpAddress is always null in DNX RC1 Update1 (bug).
if (ip.IsNullOrWhitespace() && _httpContextAccessor.HttpContext?.Connection?.RemoteIpAddress != null)
ip = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
if (ip.IsNullOrWhitespace())
ip = GetHeaderValueAs<string>("REMOTE_ADDR");
// _httpContextAccessor.HttpContext?.Request?.Host this is the local host.
if (ip.IsNullOrWhitespace())
throw new Exception("Unable to determine caller's IP.");
return ip;
}
public T GetHeaderValueAs<T>(string headerName)
{
StringValues values;
if (_httpContextAccessor.HttpContext?.Request?.Headers?.TryGetValue(headerName, out values) ?? false)
{
string rawValues = values.ToString(); // writes out as Csv when there are multiple.
if (!rawValues.IsNullOrWhitespace())
return (T)Convert.ChangeType(values.ToString(), typeof(T));
}
return default(T);
}
public static List<string> SplitCsv(this string csvList, bool nullOrWhitespaceInputReturnsNull = false)
{
if (string.IsNullOrWhiteSpace(csvList))
return nullOrWhitespaceInputReturnsNull ? null : new List<string>();
return csvList
.TrimEnd(',')
.Split(',')
.AsEnumerable<string>()
.Select(s => s.Trim())
.ToList();
}
public static bool IsNullOrWhitespace(this string s)
{
return String.IsNullOrWhiteSpace(s);
}
假设_httpContextAccessor
是通过DI提供的。
您可以使用IHttpConnectionFeature
获取此信息。
var remoteIpAddress = httpContext.GetFeature<IHttpConnectionFeature>()?.RemoteIpAddress;
httpContext.GetFeature<IHttpConnectionFeature>()
始终是null
。
var remoteIpAddress = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress;
HttpContext.Connection.RemoteIpAddress
。
在ASP.NET 2.1中,在StartUp.cs中添加以下服务:
services.AddHttpContextAccessor();
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
然后做3步:
在您的MVC控制器中定义一个变量
private IHttpContextAccessor _accessor;
DI进入控制器的构造函数
public SomeController(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
检索IP地址
_accessor.HttpContext.Connection.RemoteIpAddress.ToString()
这是完成的方式。
::1
在IPv6中是localhost。相当于127.0.0.1
就我而言,我拥有在DigitalOcean上运行的DotNet Core 2.2 Web App,并使用docker和nginx作为反向代理。使用Startup.cs中的这段代码,我可以获得客户端IP
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.All,
RequireHeaderSymmetry = false,
ForwardLimit = null,
KnownNetworks = { new IPNetwork(IPAddress.Parse("::ffff:172.17.0.1"), 104) }
});
:: ffff:172.17.0.1是我在使用之前获得的IP
Request.HttpContext.Connection.RemoteIpAddress.ToString();
在.net核心中获取ipaddress和主机名
将此代码放入控制器
跟着这些步骤:
var addlist = Dns.GetHostEntry(Dns.GetHostName());
string GetHostName = addlist.HostName.ToString();
string GetIPV6 = addlist.AddressList[0].ToString();
string GetIPV4 = addlist.AddressList[1].ToString();
我发现,有些人发现您获得的IP地址是::: 1或0.0.0.1
之所以会出现此问题,是因为您尝试从自己的计算机上获取IP,以及C#试图返回IPv6的困惑。
因此,我实现了@Johna(https://stackoverflow.com/a/41335701/812720)和@David(https://stackoverflow.com/a/8597351/812720)的答案,感谢他们!
并在这里解决:
在您的引用(依赖项/包)中添加Microsoft.AspNetCore.HttpOverrides包
在Startup.cs中添加此行
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// your current code
// start code to add
// to get ip address
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
// end code to add
}
要获取IPAddress,请在您的任何Controller.cs中使用此代码
IPAddress remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress;
string result = "";
if (remoteIpAddress != null)
{
// If we got an IPV6 address, then we need to ask the network for the IPV4 address
// This usually only happens when the browser is on the same machine as the server.
if (remoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
{
remoteIpAddress = System.Net.Dns.GetHostEntry(remoteIpAddress).AddressList
.First(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
}
result = remoteIpAddress.ToString();
}
现在您可以从remoteIpAddress或result获取IPv4地址
试试这个。
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip.ToString();
}
}
在负载均衡器后面运行.NET core
(3.1.4)IIS
不能与其他建议的解决方案一起使用。
手动读取X-Forwarded-For
标头确实如此。
IPAddress ip;
var headers = Request.Headers.ToList();
if (headers.Exists((kvp) => kvp.Key == "X-Forwarded-For"))
{
// when running behind a load balancer you can expect this header
var header = headers.First((kvp) => kvp.Key == "X-Forwarded-For").Value.ToString();
ip = IPAddress.Parse(header);
}
else
{
// this will always have a value (running locally in development won't have the header)
ip = Request.HttpContext.Connection.RemoteIpAddress;
}
我正在使用托管捆绑包运行一个自包含的软件包。
在Ubuntu上的Traefik反向代理后面运行ASP.NET Core 2.1,我需要KnownProxies
在安装官方Microsoft.AspNetCore.HttpOverrides
软件包后设置其网关IP
var forwardedOptions = new ForwardedHeadersOptions {
ForwardedHeaders = ForwardedHeaders.XForwardedFor,
};
forwardedOptions.KnownProxies.Add(IPAddress.Parse("192.168.3.1"));
app.UseForwardedHeaders(forwardedOptions);
根据文档,如果反向代理未在本地主机上运行,则这是必需的。该docker-compose.yml
Traefik已经分配了一个静态IP地址:
networks:
my-docker-network:
ipv4_address: 192.168.3.2
另外,应该足以确保在此处定义了已知的网络以在.NET Core中指定其网关。
httpContext.GetFeature<IHttpConnectionFeature>().RemoteIpAddress