欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-16 09:46:40
本文实例讲述了c#获取真实ip地址实现方法,分享给大家供大家参考。具体实现方法如下: 通常来说,大家获取用户ip地址常用的方法是: 复制代码 代码如下:string i...

本文实例讲述了c#获取真实ip地址实现方法,分享给大家供大家参考。具体实现方法如下:

通常来说,大家获取用户ip地址常用的方法是:

复制代码 代码如下:
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的方法

复制代码 代码如下:
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#程序设计有所帮助。

上一篇:

下一篇: