對于一些企業内部核心系統,特别是外網通路的時候,為了資訊安全,可能需要對外部通路的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,歡迎各位加入交流。 |