大家获取用户IP地址常用的方法是

 
C# 代码   复制

string IpAddress = "";

if((HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]!=null
&& HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] !=String.Empty) )

{

IpAddress=HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ;

}

else

{

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

}

事实上,上面的代码只试用与用户只使用了1层代理,如果用户有2层,3层HTTP_X_FORWARDED_FOR 的值是:"本机真实IP,1层代理IP,2层代理IP,....." ,如果这个时候你的数据中保存IP字段的长度很小(15个字节),数据库就报错了。

实际应用中,因为使用多层透明代理的情况比较少,所以这种用户并不多。

获取用户真实IP的方法

 
C# 代码   复制
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
namespace Common
{
/// <summary>
/// IPAddress 的摘要说明
/// </summary>
public class IPAddress : System.Web.UI.Page
{

public static Int64 toDenaryIp ( string ip )
{
Int64 _Int64 = 0;
string _ip = ip;
if ( _ip.LastIndexOf ( "." ) > -1 )
{
string[] _iparray = _ip.Split ( '.' );

_Int64 = Int64.Parse ( _iparray.GetValue ( 0 ).ToString() ) * 256 * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 1 ).ToString() ) * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 2 ).ToString() ) * 256 + Int64.Parse ( _iparray.GetValue ( 3 ).ToString() ) - 1;
}
return _Int64;
}

/// <summary>
/// /ip十进制
/// </summary>
public static Int64 DenaryIp
{
get {
Int64 _Int64 = 0;

string _ip = IP;
if ( _ip.LastIndexOf ( "." ) > -1 )
{
string[] _iparray= _ip.Split ( '.' );

_Int64 = Int64.Parse ( _iparray.GetValue ( 0 ).ToString() ) * 256 * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 1 ).ToString() ) * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 2 ).ToString() ) * 256 + Int64.Parse ( _iparray.GetValue ( 3 ).ToString() )-1;
}
return _Int64;
}
} public static string IP
{
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;
}
} //是否ip格式
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 );
} }
}

C#如何获取真实IP地址的更多相关文章

  1. 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址;

    package com.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.htt ...

  2. JSP 获取真实IP地址的代码

    [转载]JSP 获取真实IP地址的代码 JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.   ...

  3. 【转载】JSP 获取真实IP地址的代码

    JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.   但是在通过了 Apache,Squid ...

  4. java 获取真实ip地址

    /** * 获取真实ip地址 * @param request * @return */ public static String getIpAddress(HttpServletRequest re ...

  5. C#获取真实IP地址实现方法

    通常来说,大家获取用户IP地址常用的方法是: string IpAddress = ""; if((HttpContext.Current.Request.ServerVariab ...

  6. java 客户端获取真实ip地址

    在开发工作中,我们常常需要获取客户端的IP.一般获取客户端的IP地址的方法是:request.getRemoteAddr();但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实 ...

  7. php获取真实ip地址(转)

    REMOTE_ADDR只能获取访问者本地连接中设置的IP,如中南民族大学校园网中自己设置的10.X.XXX.XXX系列IP,而这个函数获取的是局域网网关出口的IP地址, 如果访问者使用代理服务器,将不 ...

  8. Nginx反向代理 Laravel获取真实IP地址(PHP)

    使用VUE前后端分离开发 后端使用Laravel  想要获取到用户的真实IP地址 因为分离开发不同源跨域问题 所以只能进行前端Nginx反向代理 location /api { rewrite ^/a ...

  9. 获取客户机MAC地址 根据IP地址 获取机器的MAC地址 / 获取真实Ip地址

    [DllImport("Iphlpapi.dll")] private static extern int SendARP(Int32 dest, Int32 host, ref ...

随机推荐

  1. UTF-8/UTF-16/UTF-32

    UTF-8/UTF-16/UTF-32 一.UTF-8/UTF-16/UTF-32三者的区别 二.BOM的检测与删除 1.用VIM去除<feff>,即 U+FEFF.注意:这是一个字符,而 ...

  2. 学习C++ Primer 的个人理解(十一)

    关联容器 就像是个字典, 其元素是 键 - 值 对. 关键字起到索引作用. 有序: map:关联数组:保存 健-值 对 set : 关键字既是值. multimap : 关键字可重复出现的map mu ...

  3. WIX: Hide installed program from the Add/Remove Programs window.

    Reference article : How to hide an entry in the Add/Remove Programs applet? In Wix source files, set ...

  4. [翻译][MVC 5 + EF 6] 5:Code First数据库迁移与程序部署

    原文:Code First Migrations and Deployment with the Entity Framework in an ASP.NET MVC Application 1.启用 ...

  5. Java 使用反射拷贝对象一般字段值

    在<Java解惑>上面看到第八十三例--诵读困难者,要求使用非反射实现单例对象的拷贝.查阅了部分资料,先实现通过反射拷贝对象. 1. 编写需要被拷贝的对象Person package co ...

  6. Hadoop常见的45个问题解答

    (大讲台:国内首个it在线教育混合式自适应学习) 1.Hadoop集群可以运行的3个模式 单机(本地)模式 伪分布式模式 全分布式模式 2.  单机(本地)模式中的注意点? 在单机模式(standal ...

  7. ModelState用法

    ModelState.AddModelError:添加错误信息 ModelState是一个字典类型,这句话的作用是向ModelState中添加一条错误信息,第一个参数是Key,第二个参数是Value. ...

  8. python mongodb MapReduce

    # -*- coding: utf-8 -*-import osimport csvimport pymongofrom pymongo import MongoClientfrom bson.cod ...

  9. js动态添加id

    <script type="text/javascript"> function add_id(){ var dlall=document.getElementsByT ...

  10. [JavaScript] 怎么使用JS禁止复制粘贴

    1. 将彻底屏蔽鼠标右键,其实是禁止快捷菜单,因为不光右键可以弹出这个菜单,键盘上空格键右边的windows键也可以激活这个快捷菜单 <table border oncontextmenu=re ...