一種基於自定義代碼的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,歡迎各位加入交流


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM