.NET 客户IP地址捕捉
2024-09-29 20:11:09
MVC模式下要获取客户IP可以在ActionFilterAttribute中进行拦截
filterContext.HttpContext.Request.UserHostAddress
同样,在WebAPI中也可以用同样的方式获取,只是继承自System.Web.Http.Filters.ActionFilterAttribute
private string GetClientIP(HttpActionContext actionContext)
{
if (actionContext.Request.Properties.ContainsKey("MS_HttpContext"))
{
return ((HttpContextWrapper)actionContext.Request.Properties["MS_HttpContext"]).Request.UserHostAddress;
} if (actionContext.Request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
RemoteEndpointMessageProperty prop;
prop = (RemoteEndpointMessageProperty)actionContext.Request.Properties[RemoteEndpointMessageProperty.Name];
return prop.Address;
} return null;
}
最新文章
- 转 C# 只允许运行一个实例
- Android之自定义ViewGroup
- JavaScript实现样式表的简单切换
- Deep Learning for NLP 文章列举
- javascript封装自定义滚动条方法,可自定义四个边框滚动条
- [译]Java 设计模式之命令
- echarts仪表盘如何设置图例(legend)
- 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——实现篇:(四)用户接口层之处理SDP报文
- [php]通过http post发送json数据
- 使用Netbeans内置的Git工具
- hdu1003 Max Sum---最大子段和+记录开始结束点
- Java基础 -- 连接字符串时,使用+还是StringBuilder
- optional install error: Package require os(darwin) not compatible with your platform(win32)
- Java中线程的同步问题
- 二 分析easyswoole源码(启动服务)
- Matconvnet 的一些记录
- mysql分区分表讲解
- ac1067
- ubuntu下screen的使用
- Codeforces Round #415 (Div. 2)C