怎么获取用户登录的IP地址???

adu_ado 2010-07-01 05:11:29
我想记录用户操作IP地址
  怎么获取??
一:this.Page.Request.UserHostAddress.ToString();
二: public static string GetIPAddress()
{

string user_IP = string.Empty;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
user_IP = System.Web.HttpContext.Current.Request.UserHostAddress;
}
}
else
{
user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
return user_IP;
}
还有什么方法,获取最真实的IP地址

...全文
1619 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuaiqiwangzi 2012-03-02
  • 打赏
  • 举报
回复
楼上给的code真心不错~
宇峰科技 2010-07-02
  • 打赏
  • 举报
回复
//给你一个比较全的吧 ,能够穿多层代理的,也是引用人的啊

public static string IPAddress
{
get
{
string result = String.Empty;

result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (result != null && result != String.Empty)
{
//可能有代理
if (result.IndexOf(".") == -1) //没有“.”肯定是非IPv4格式
result = null;
else
{
if (result.IndexOf(",") != -1)
{
//有“,”,估计多个代理。取第一个不是内网的IP。
result = result.Replace(" ", "").Replace("'", "");
string[] temparyip = result.Split(",;".ToCharArray());
for (int i = 0; i < temparyip.Length; i++)
{
if ( IsIPAddress(temparyip[i])
&& temparyip[i].Substring(0, 3) != "10."
&& temparyip[i].Substring(0, 7) != "192.168"
&& temparyip[i].Substring(0, 7) != "172.16.")
{


return temparyip[i]; //找到不是内网的地址
}
}
}
else if ( IsIPAddress(result)) //代理即是IP格式
return result;
else
result = null; //代理中的内容 非IP,取IP
}

}

string IpAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty) ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

if (null == result || result == String.Empty)
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

if (result == null || result == String.Empty)
result = HttpContext.Current.Request.UserHostAddress;

return result;
}
}

public static bool IsIPAddress(string str1)
{
if (str1 == null || str1 == string.Empty || str1.Length < 7 || str1.Length > 15) return false;
string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$";


Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
return regex.IsMatch(str1);
}
adu_ado 2010-07-02
  • 打赏
  • 举报
回复
呵呵 多谢建议~~~
lunhuifengzheng 2010-07-01
  • 打赏
  • 举报
回复
和LZ用的一样的方法
chw053167 2010-07-01
  • 打赏
  • 举报
回复
string ip = Request.ServerVariables["Remote_Addr"];
string ip = Request.UserHostAddress
xiaoxiao8372 2010-07-01
  • 打赏
  • 举报
回复
我还直不清楚,在这里学习了啊。
mchaojie1 2010-07-01
  • 打赏
  • 举报
回复
string ip1 = Request.UserHostAddress;
string ip2 = Request.ServerVariables["Remote_Addr"];
stepday 2010-07-01
  • 打赏
  • 举报
回复
Request.UserHostAddress.ToString() 就可以获得登录客户的IP地址的
gdlpc 2010-07-01
  • 打赏
  • 举报
回复
Request.UserHostAddress.ToString()
robake 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhengfujie 的回复:]
string ip = Request.ServerVariables["Remote_Addr"];
string ip = Request.UserHostAddress;
[/Quote]


足够用了~
yypf2540017 2010-07-01
  • 打赏
  • 举报
回复
知道的也就lz这样的了
public static string GetIP()
{
string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.UserHostAddress;

if (string.IsNullOrEmpty(result) || !Utils.IsIP(result))
return "127.0.0.1";

return result;
}
sbc_86 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 adu_ado 的回复:]
我想记录用户操作IP地址
  怎么获取??
一:this.Page.Request.UserHostAddress.ToString();
二: public static string GetIPAddress()
{

string user_IP = string.Empty;
if (System.Web.HttpContext.Current.Request.S……
[/Quote]
....
「已注销」 2010-07-01
  • 打赏
  • 举报
回复
string ip = Request.ServerVariables["Remote_Addr"];
string ip = Request.UserHostAddress;

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧