请问如何在ASP中获取客户端IP地址?NET时使用MVC 6。 请求。ServerVariables["REMOTE_ADDR"]无效。
当前回答
根据官方文档,如果你使用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和处理无效报头
其他回答
在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。
您可以使用IHttpConnectionFeature来获取此信息。
var remoteIpAddress = httpContext.GetFeature<IHttpConnectionFeature>()?.RemoteIpAddress;
在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 ()
事情是这样的。
也可以从外部服务获取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; }
}
从这个环节,就有了更好的解决方案。
在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;
推荐文章
- 函数应该返回空对象还是空对象?
- 如何转换日期时间?将日期时间
- 如何在c#中连接列表?
- 在c#中引用类型变量的“ref”的用途是什么?
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 转换为值类型'Int32'失败,因为物化值为空
- c#中有任何连接字符串解析器吗?
- 在Linq中转换int到字符串到实体的问题
- 是否可以动态编译和执行c#代码片段?
- 创建自定义MSBuild任务时,如何从c#代码获取当前项目目录?
- c#和Java的主要区别是什么?
- 在c#中创建一个特定时区的DateTime
- .NET中的属性是什么?
- csproj文件中的“Service Include”是干什么用的?
- 如何使用try catch进行异常处理是最佳实践