请问如何在ASP中获取客户端IP地址?NET时使用MVC 6。 请求。ServerVariables["REMOTE_ADDR"]无效。
您可以使用IHttpConnectionFeature来获取此信息。
var remoteIpAddress = httpContext.GetFeature<IHttpConnectionFeature>()?.RemoteIpAddress;
var remoteIpAddress = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress;
API已经更新。不知道它什么时候改变了,但根据达米安·爱德华兹在12月底的说法,你现在可以这样做:
var remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;
可以添加一些回退逻辑来处理负载均衡器的存在。
此外,通过检查,即使没有负载均衡器,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提供。
在项目。Json添加一个依赖:
"Microsoft.AspNetCore.HttpOverrides": "2.2.0"
在Startup.cs中,在Configure()方法中添加:
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto
});
当然,还有:
using Microsoft.AspNetCore.HttpOverrides;
然后,我可以通过使用:
Request.HttpContext.Connection.RemoteIpAddress
在我的情况下,在VS中调试时,我总是得到IpV6 localhost,但在IIS上部署时,我总是得到远程IP。
一些有用的链接: 如何在ASP中获取客户端IP地址。网络核心?RemoteIpAddress为空
::1可能是因为:
连接终止在IIS,然后转发到Kestrel, v.next web服务器,因此连接到web服务器确实是从本地主机。(https://stackoverflow.com/a/35442401/5326387)
编辑12/2020:感谢SolidSnake:截至2020年12月,最新版本是2.2.0
Edit 06/2021:感谢Hakan fakhtik:在。net 5中,命名空间是Microsoft.AspNetCore.Builder
首先,在。net Core 1.0中 使用Microsoft.AspNetCore.Http.Features添加;到控制器 然后里面的相关方法:
var ip = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress?.ToString();
我读了其他几个答案,因为它使用了小写的httpContext,导致VS使用Microsoft.AspNetCore添加。Http,而不是适当的使用,或与HttpContext(编译器也是误导)。
在ASP。NET 2.1,在StartUp.cs中添加此服务:
services.AddHttpContextAccessor();
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
然后做3步:
在MVC控制器中定义一个变量 private IHttpContextAccessor _accessor DI转换为控制器的构造函数 IHttpContextAccessor访问器 { _accessor = accessor; } 检索IP地址 _accessor.HttpContext.Connection.RemoteIpAddress.ToString ()
事情是这样的。
这适用于我(DotNetCore 2.1)
[HttpGet]
public string Get()
{
var remoteIpAddress = HttpContext.Connection.RemoteIpAddress;
return remoteIpAddress.ToString();
}
在我的情况下,我用docker和nginx作为反向代理在DigitalOcean上运行DotNet Core 2.2 Web应用程序。使用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 Core中获取IP地址和主机名,在控制器中输入以下代码:
var addlist = Dns.GetHostEntry(Dns.GetHostName());
string GetHostName = addlist.HostName.ToString();
string GetIPV6 = addlist.AddressList[0].ToString();
string GetIPV4 = addlist.AddressList[1].ToString();
试试这个。
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip.ToString();
}
}
我发现,你们中的一些人发现你们得到的IP地址是:::1或0.0.0.1
这是一个问题,因为你试图从你自己的机器获取IP,而c#试图返回IPv6的混乱。
所以,我实现了来自@Johna (https://stackoverflow.com/a/41335701/812720)和@David (https://stackoverflow.com/a/8597351/812720)的答案,感谢他们!
下面是解决方案:
add Microsoft.AspNetCore.HttpOverrides Package in your References (Dependencies/Packages) add this line in 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 } to get IPAddress, use this code in any of your 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地址
在IIS上使用负载均衡器运行。net core(3.1.4)不能与其他建议的解决方案一起工作。
手动读取X-Forwarded-For报头可以。 这段代码假设这个报头包含一个IP。
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();
// in case the IP contains a port, remove ':' and everything after
ip = IPAddress.Parse(header.Remove(header.IndexOf(':')));
}
else
{
// this will always have a value (running locally in development won't have the header)
ip = Request.HttpContext.Connection.RemoteIpAddress;
}
感谢@JawadAlShaikh和@BozoJoe指出IP可以包含一个端口,x - forward - for可以包含多个IP。
运行ASP。在Ubuntu上的Traefik反向代理后面的NET Core 2.1,我需要在安装官方的Microsoft.AspNetCore.HttpOverrides包后在knownproxy中设置它的网关IP
var forwardedOptions = new ForwardedHeadersOptions {
ForwardedHeaders = ForwardedHeaders.XForwardedFor,
};
forwardedOptions.KnownProxies.Add(IPAddress.Parse("192.168.3.1"));
app.UseForwardedHeaders(forwardedOptions);
根据文档,如果反向代理没有在本地主机上运行,这是必需的。docker-compose。Traefik . yml已分配静态IP地址:
networks:
my-docker-network:
ipv4_address: 192.168.3.2
或者,它应该足以确保在这里定义了一个已知的网络,并在. net Core中指定其网关。
在。net 5中,我使用它通过AWS fargate上的容器来检索客户端IP。
public static class HttpContextExtensions
{
//https://gist.github.com/jjxtra/3b240b31a1ed3ad783a7dcdb6df12c36
public static IPAddress GetRemoteIPAddress(this HttpContext context, bool allowForwarded = true)
{
if (allowForwarded)
{
string header = (context.Request.Headers["CF-Connecting-IP"].FirstOrDefault() ?? context.Request.Headers["X-Forwarded-For"].FirstOrDefault());
if (IPAddress.TryParse(header, out IPAddress ip))
{
return ip;
}
}
return context.Connection.RemoteIpAddress;
}
}
你这样称呼它:
var ipFromExtensionMethod = HttpContext.GetRemoteIPAddress().ToString();
源
从这个环节,就有了更好的解决方案。
在Startup.cs中,我们需要添加service-
public void ConfigureServices(IServiceCollection services)
{
........
services.AddHttpContextAccessor();
........
}
然后在任何控制器或任何地方,我们都需要像这样通过依赖注入来使用它
private IHttpContextAccessor HttpContextAccessor { get; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IWebHostEnvironment env, IHttpContextAccessor httpContextAccessor)
: base(options)
{
Environment = env;
HttpContextAccessor = httpContextAccessor;
//this.Database.EnsureCreated();
}
然后得到这样的IP
IPAddress userIp = HttpContextAccessor.HttpContext.Connection.RemoteIpAddress;
@crokusek回答的简短版本
public string GetUserIP(HttpRequest req)
{
var ip = req.Headers["X-Forwarded-For"].FirstOrDefault();
if (!string.IsNullOrWhiteSpace(ip)) ip = ip.Split(',')[0];
if (string.IsNullOrWhiteSpace(ip)) ip = Convert.ToString(req.HttpContext.Connection.RemoteIpAddress);
if (string.IsNullOrWhiteSpace(ip)) ip = req.Headers["REMOTE_ADDR"].FirstOrDefault();
return ip;
}
截至2021年9月- ASP。NET Core (5.x) MVC项目允许我在我的控制器中以这种方式获取IP地址:
Request.HttpContext.Connection.RemoteIpAddress
现在似乎比过去简单多了。
根据官方文档,如果你使用Apache或Nginx集成,以下代码应该添加到启动。ConfigureServices方法。
// using Microsoft.AspNetCore.HttpOverrides;
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto;
// Only loopback proxies are allowed by default.
// Clear that restriction because forwarders are enabled by explicit
// configuration.
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
最重要的是,在配置方法中使用
app.UseForwardedHeaders();
进一步假设在nginx conf文件中,在一个位置内,使用
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
现在X-Forwarded-For中的第一个条目将是真正的客户端IP。
重要:如果你想保护应用程序,不允许攻击者注入x - forward - for,请阅读这个答案。
请参见转发Linux和非iis反向代理方案、配置Nginx和处理无效报头
第一次添加
Microsoft.AspNetCore.Http
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
在Startup.cs中的ConfigureServices中 然后在控制器中添加以下代码
private IHttpContextAccessor _accessor;
public LoginController(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
public IEnumerable<string> Get()
{
var ip = _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
return new string[] { ip, "value" };
}
希望这对你有用
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
public string GetClientIPAddress(HttpContext context)
{
string ip = string.Empty;
if (!string.IsNullOrEmpty(context.Request.Headers["X-Forwarded-For"]))
{
ip = context.Request.Headers["X-Forwarded-For"];
}
else
{
ip = context.Request.HttpContext.Features.Get<IHttpConnectionFeature>().RemoteIpAddress.ToString();
}
return ip;
}
你想获取Ip地址;
GetClientIPAddress(HttpContext);
试试这个:
string remoteHost = $"{httpContext.Connection.RemoteIpAddress}:{httpContext.Connection.RemotePort}";
也可以从外部服务获取IP。
public string GetIP()
{
HttpClient client = new HttpClient();
var result = client.GetStringAsync("https://jsonip.com/").Result;
var ip = JsonSerializer.Deserialize<RemoteIPDto>(result.ToString()).IP;
return ip;
}
RemoteIPDto类在哪里
public class RemoteIPDto
{
[JsonPropertyName("ip")]
public string IP { get; set; }
[JsonPropertyName("geo-ip")]
public string GeoIp { get; set; }
[JsonPropertyName("API Help")]
public string ApiHelp { get; set; }
}
您还可以使用api.db-ip.com web服务获取IP地址和位置。
这项服务是免费的,但有一个限制:每天1000次请求。
public string GetRemoteIP()
{
HttpClient client = new HttpClient();
var result = client.GetStringAsync("https://api.db-ip.com/v2/free/self").Result;
var ip = JsonSerializer.Deserialize<RemoteIPDto>(result.ToString()).IP;
return ip;
}
public static string GetUserAddress()
{
HttpClient client = new HttpClient();
var result = client.GetStringAsync("https://api.db-ip.com/v2/free/self").Result;
var location = result.ToString();
return remoteAddress;
}
RemoteIPDto类在哪里
public class RemoteIPDto
{
[JsonPropertyName("ipAddress")]
public string IP { get; set; }
[JsonPropertyName("continentCode")]
public string ContinentCode { get; set; }
[JsonPropertyName("continentName")]
public string ContinentName { get; set; }
[JsonPropertyName("countryCode")]
public string CountryCode { get; set; }
[JsonPropertyName("countryName")]
public string CountryName { get; set; }
[JsonPropertyName("city")]
public string City { get; set; }
}
注意:当使用localhost时,IP地址总是“0.0.0.1”,但当我在AWS EC2实例上使用Nginx托管应用程序时,我收到了正确的IP地址。
将以下包添加到您的项目中:
"Microsoft.AspNetCore.HttpOverrides": "2.2.0"
然后在Startup.cs中的Configure()方法中添加以下内容(确保将其放在app.UseStaticFiles()和app.UseRouting()之前)
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
// app.UseStaticFiles();
// app.UseRouting();
然后在你的控制器类中,你可以使用下面的代码获取IP地址:
IPAddress remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4()?.ToString();