一种基于自定义代码的asp.net网站访问IP过滤方法!

         对于一些企业内部核心系统,特别是外网访问的时候,为了信息安全,可能需要对外部访问的IP地址作限制,虽然IIS中也提供了根据IP地址或IP地址段进行限制或允许,但并没有提供根据IP地址所在的城市进行限制或允许。本文主要通过自定义扩展IHttpModule接口,考虑到性能IP数据库主要采用QQwry纯真IP数据库(但此数据库并非是官方的,我之前与ip138网站对比过,IP地址信息的准确性大概在90%左右),主要实现不仅可以根据IP地址或IP地址段进行限制或允许(与IIS的功能相同),而且可以根据IP地址的所在城市进行限制或允许。该WebsiteFilter组件核心代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
using System.IO;
using System.Net;
using NetOpen_System.Component.QQWry;

namespace NetOpen_System.Component
{
    public sealed class WebsiteFilterHttpModule : IHttpModule
    {
        #region IHttpModule 成员

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }


        #endregion


        void context_BeginRequest(object sender, EventArgs e)
        {
            try
            {
                //HttpApplication objApp = (HttpApplication)sender;

                if (HttpContext.Current.Request.IsLocal)//忽略本地计算机请求
                    return;

                string ip = HttpContext.Current.Request.UserHostAddress;
               

                QQWryLocator qqWry = new QQWryLocator(HttpContext.Current.Server.MapPath(@"~\IpData\qqwry.dat"));

                IPLocation ipaddress = qqWry.Query(ip);  //查询一个IP地址

                UrlMatchEngine pu = WebsiteFilterConfiguration.GetConfig().PickedUrls;


                if (string.IsNullOrEmpty(pu.CitySiteList) == false)
                {
                    if (pu.CitySiteList.Contains(ipaddress.Country) == false)
                    {

                        if (!WebsiteFilterConfiguration.GetConfig().IpChecks.GetIpIn(ip))
                        {   //若在IP列表中找不到访客ip

                            //string rawUrl = HttpContext.Current.Request.RawUrl;
                            //UrlMatchEngine pu = WebsiteFilterConfiguration.GetConfig().PickedUrls;

                            ////列表包含当前url且列表为黑名单、列表不包含当前url且列表不为黑名单  时需转向
                            ////换而言之,“配备结果”与“是否黑名单”取值一致时需转向
                            //if (pu.IsMatch(rawUrl) == pu.IsBlacklist)
                            //{   //非公开url自动重定向
                            //    HttpContext.Current.Response.Redirect(pu.ErrorPage);
                            //}
                            HttpContext.Current.Response.Redirect(pu.ErrorPage, true);
                            //HttpContext.Current.Server.Transfer(pu.ErrorPage);

                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    if (!WebsiteFilterConfiguration.GetConfig().IpChecks.GetIpIn(ip))
                    {   //若在IP列表中找不到访客ip

                        //string rawUrl = HttpContext.Current.Request.RawUrl;
                        //UrlMatchEngine pu = WebsiteFilterConfiguration.GetConfig().PickedUrls;

                        ////列表包含当前url且列表为黑名单、列表不包含当前url且列表不为黑名单  时需转向
                        ////换而言之,“配备结果”与“是否黑名单”取值一致时需转向
                        //if (pu.IsMatch(rawUrl) == pu.IsBlacklist)
                        //{   //非公开url自动重定向
                        //    HttpContext.Current.Response.Redirect(pu.ErrorPage);
                        //}
                        HttpContext.Current.Response.Redirect(pu.ErrorPage, true);
                        //HttpContext.Current.Server.Transfer(pu.ErrorPage);
                    }
                    else
                    {
                        return;
                    }
                }
            }
            catch
            {

            }
        }
    }
}

 

            在部署方面,非常简单主要利用IHttpModule接口并在Web.config中的HttpModule节点添加此组件的配置,访问限制或允许参数可以在NetOpen_SystemWebsiteFilter.cfg.xml进行设置,以下为一个简单的配置示例;

<?xml version="1.0" encoding="utf-8" ?>
<NetOpen_System>
  <WebsiteFilter>
    <PickedUrl IsBlacklist="0" ErrorPage="~/sorry.htm" CitySiteList="浙江省宁波市,浙江省杭州市">
      <add pattern="^~/default.aspx"/>
    </PickedUrl>
    <PickedIP>
      <add ip1="192.168.10.1" ip2="192.168.10.5" />
      <remove ip1="192.168.10.2" ip2="192.168.10.4" />
      <add ip1="192.168.10.3" />
    </PickedIP>
  </WebsiteFilter>
</NetOpen_System>

            该组件源代码下载地址:https://websitefilter.codeplex.com/,欢迎访问下载!虽然该组件实现并不复杂,原理也很简单,但较为实用,后续将增加根据IP138的网站进行实时查询,这样IP地址信息将更为精确,但对性能可能会有一些影响。

 

本博客为软件人生原创,欢迎转载,转载请标明出处:http://www.cnblogs.com/nbpowerboy/p/3160134.html。演绎或用于商业目的,但是必须保留本文的署名软件人生(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。SharePoint商业智能技术QQ群:140668362,.Net技术交流QQ群:195516928,欢迎各位加入交流

posted @ 2013-06-28 09:10  软件人生  阅读(3200)  评论(0编辑  收藏  举报