如果不熟悉HttpModule的话,可以去MSDN上查阅,介绍非常详细,地址:ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.
PRivate void Application_BeginRequest(Object source, Event
args e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
bool isSafe = true; //
是否合法链接
string uri = context.Request.Url.AbsolutePath.ToLower();
if (uri.LastIndexOf(“.”) > 0
&& context.Request.UrlReferrer != null)
{
string e
xp = uri.S
ubstring(uri.LastIndexOf(“.”));
//这里是判断
文件后缀名是否在排除的
文件类型列表之内
bool isHas = ClassLi
brary.RData.RString.StrIsIncUseSC(exp,
config.
imgSafeTy
PE.Spl
IT(‘|'));
if (isHas)
{
string dom
ainOutter = context.Request.UrlReferrer.Authority.ToLower(); //包含域名和端口
ArrayList arry = Common.Cache.GetDomainValid();//取系统定义的合法的
域名绑定列表
isSafe = arry.Contains(domainOutter); //判断当前请求的域名是否在合法列表之内
}
}
//下面就是不合法的时候的输出了,如果有默认替代图片则输出,如果没有就生成一个,格式为。gif
if (!isSafe)
{
Bitmap img = null;
Graphics g = null;
MemoryStre
am ms = null;
try
{
string picPath = ClassLibrary.RPath.GetFullDirectory(“images/unlawful.gif”);
if (File.Exists(picPath))
{
img = new Bitmap(picPath, false);
}
else
{
img = new Bitmap(**, **);
g = Graphics.
FromImage(img);
g.Clear(Color.White);
Font f = new Font(“
宋体,黑体,Arial”, 9,FontStyle.Bold);
SolidBrush s = new SolidBrush(Color.
red);
g.DrawString(Resources.Message.LawlessLink, f, s, 1, 20);
img.Save(picPath, ImageFormat.Gif);
}
ms = new MemoryStream();
img.Save(ms, ImageFormat.Gif);
context.Response.ClearContent();
context.Response.ContentType = “image/Gif”;
context.Response.BinaryWrite(ms.ToArray());
context.Response.End();
}
catch
{ }
finally
{
if(g != null )
g.Dispose();
img.Dispose();
}
}
}
if (HttpContext.
current.Request.UrlReferrer != null)
{
if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith("
jpg", StringComparison.OrdinalIgnoreCase) &am
p;& HttpContext.Current.Request.UrlReferrer.Host != "localhost")
{
HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath("~/jzdl.jpg"));
HttpContext.Current.Response.End();
}
}