这是EntLibForum论坛系统4.0的判断是否为禁用IP的部分,小弟有点看不明白作者的思路是什么?请大侠指教???第一部分代码:
try
{
string key = string.Format( "BannedIP.{0}", PageBoardID );
DataTable banip = ( DataTable ) HttpContext.Current.Cache [key];
问题一:为什么上来就取缓存里的信息?“PageBoardID”指什么,大侠能给猜测下吗???

if ( banip == null )
{
banip = DB.bannedip_list( PageBoardID, null );
HttpContext.Current.Cache [key] = banip;
}
foreach ( DataRow row in banip.Rows )
if ( Utils.IsBanned( ( string ) row ["Mask"], HttpContext.Current.Request.ServerVariables ["REMOTE_ADDR"] ) )
HttpContext.Current.Response.End();
问题二:
MSDN的解释是:将当前所有缓冲的输出发送到客户端,停止该页的执行,并引发 EndRequest 事件。如果是禁用的IP,这行代码的作用是什么,停止访问吗?

}
catch ( Exception )
{
// If the above fails chances are that this is a new install
Response.Redirect( Data.ForumRoot + "default/" );
}第二部分代码:
/// <summary>
/// List of Baned IP's
/// </summary>
/// <param name="boardID">ID of board</param>
/// <param name="ID">ID</param>
/// <returns>DataTable of banned IPs</returns>
static public DataTable bannedip_list( object boardID, object ID )
{
using ( SqlCommand cmd = new SqlCommand( "yaf_bannedip_list" ) )
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( "@BoardID", boardID );
cmd.Parameters.AddWithValue( "@ID", ID );
return GetData( cmd );
}
}
第三部分代码:
static public bool IsBanned( string ban, string chk )问题三:这里做了怎样的处理?看不明白,做了怎样的比较判断???

{
String [] ipmask = ban.Split( '.' );
String [] ip = ban.Split( '.' );
for ( int i = 0; i < ipmask.Length; i++ )
{
if ( ipmask [i] == "*" )
{
ipmask [i] = "0";
ip [i] = "0";
}
else
ipmask [i] = "255";
} ulong banmask = Str2IP( ip );
ulong banchk = Str2IP( ipmask );
ulong ipchk = Str2IP( chk.Split( '.' ) ); return ( ipchk & banchk ) == banmask;
}
static public ulong Str2IP( String [] ip )
{
if ( ip.Length != 4 )
throw new Exception( "Invalid ip address." ); ulong num = 0;
for ( int i = 0; i < ip.Length; i++ )
{
num <<= 8;
num |= ulong.Parse( ip [i] );
问题四:“<<=”和“|=”什么意思?做了怎样的处理?

}
return num;
}

解决方案 »

  1.   

    问题1 就是BannedIP.PageBoardID  猜测就是被禁止IP下的一个ID 而这个ID是存入缓存的keyHttpContext.Current.Response.End()这个就是输出客户端结束static public bool IsBanned( string ban, string chk ) 这个方法里分割了IP 判断IP是否被禁止就是把一个IP通过.分成了4段然后判断那两个符号还真不知道   MARK
      

  2.   

    相关页面ID,保存到缓存的KEY
    Split( '.' );分割IP转long判断是否在限制范围内
    System.Net.IPAddress ipaddress = System.Net.IPAddress.Parse("");
    long ip= ipaddress.Address;直接判断ip 范围