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


    这篇文章主要介绍了C#获取真实IP地址实现方法,对比了C#获取IP地址的常用方法并实例展示了C#获取真实IP地址的方法,非常具有实用价值,需要的朋友可以参考下
     

    本文实例讲述了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"];
    }
    View Code

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

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

    获取用户真实IP的方法

      1 using System;
      2 using System.Data;
      3 using System.Configuration;
      4 using System.Web;
      5 using System.Web.Security;
      6 using System.Web.UI;
      7 using System.Web.UI.WebControls;
      8 using System.Web.UI.WebControls.WebParts;
      9 using System.Web.UI.HtmlControls;
     10 using System.Text.RegularExpressions;
     11 namespace Common
     12 {
     13     /// <summary>
     14     /// IPAddress 的摘要说明
     15     /// </summary>
     16 public class IPAddress : System.Web.UI.Page
     17 {
     18 
     19     public static Int64 toDenaryIp ( string ip )
     20     {
     21         Int64 _Int64 = 0;
     22         string _ip = ip;
     23         if ( _ip.LastIndexOf ( "." ) > -1 )
     24         {
     25             string[] _iparray = _ip.Split ( '.' );
     26 
     27             _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;
     28         }
     29         return _Int64;
     30     }
     31 
     32     /// <summary>
     33     /// /ip十进制
     34     /// </summary>
     35     public static Int64 DenaryIp
     36     {
     37         get {
     38             Int64 _Int64 = 0;
     39 
     40             string _ip = IP;
     41             if ( _ip.LastIndexOf ( "." ) > -1 )
     42             {
     43                 string[] _iparray= _ip.Split ( '.' );
     44 
     45                 _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;
     46             }
     47             return _Int64;
     48         }
     49     }
     50 
     51     public static string IP
     52     {
     53         get
     54         {
     55             string result = String.Empty;
     56             result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
     57             if ( result != null && result != String.Empty )
     58             {
     59                //可能有代理
     60                 if ( result.IndexOf ( "." ) == -1 ) //没有"."肯定是非IPv4格式
     61                     result = null;
     62                 else
     63                 {
     64                     if ( result.IndexOf ( "," ) != -1 )
     65                     {
     66                          //有",",估计多个代理。取第一个不是内网的IP。
     67                         result = result.Replace ( " ", "" ).Replace ( "", "" );
     68                         string[] temparyip = result.Split ( ",;".ToCharArray() );
     69                         for ( int i = 0; i < temparyip.Length; i++ )
     70                         {
     71                             if ( IsIPAddress ( temparyip[i] )
     72                                     && temparyip[i].Substring ( 0, 3 ) != "10."
     73                                     && temparyip[i].Substring ( 0, 7 ) != "192.168"
     74                                     && temparyip[i].Substring ( 0, 7 ) != "172.16." )
     75                             {
     76                                 return temparyip[i]; //找到不是内网的地址
     77                             }
     78                         }
     79                     }
     80                     else if ( IsIPAddress ( result ) ) //代理即是IP格式
     81                         return result;
     82                     else
     83                         result = null; //代理中的内容 非IP,取IP
     84                 }
     85             }
     86 
     87             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"];
     88           
     89             if ( null == result || result == String.Empty )
     90                 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
     91 
     92             if ( result == null || result == String.Empty )
     93                 result = HttpContext.Current.Request.UserHostAddress;
     94 
     95             return result;
     96         }
     97     }
     98 
     99      //是否ip格式
    100     public static bool IsIPAddress ( string str1 )
    101     {
    102         if ( str1 == null || str1 == string.Empty || str1.Length < 7 || str1.Length > 15 ) return false;
    103         string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$";
    104         Regex regex = new Regex ( regformat, RegexOptions.IgnoreCase );
    105         return regex.IsMatch ( str1 );
    106     }
    107 }
    108 }

    开始我也是直接获取的,没有遇到客户反馈说有其他问题,不过还是改一下的好

  • 相关阅读:
    转载:SQL server2005 里面没有management studio!下载SQL开发版本
    LInux、Ubuntu、win7、win8纯净版 镜像包链接地址 收集
    [转]Lucene经验总结 (转注:较旧,但有干货)
    几篇调试IIS内存过高或CPU过高的好文章
    Web应用程序
    使用ASP.NET State Server实现多应用程序间共享Session State
    [转]使用visual studio进行web应用程序性能测试
    [转] ASP.NET WEB API程序在VS启动或发布到IIS后启动后发生
    [转]优化Redis内存的9个要点
    [转]使用Memcached的8个要点
  • 原文地址:https://www.cnblogs.com/maanshancss/p/4074533.html
Copyright © 2020-2023  润新知