復制代碼 代碼如下:
private void Application_BeginRequest(Object source, EventArgs 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 exp = uri.Substring(uri.LastIndexOf(“.”));
//這里是判斷文件后綴名是否在排除的文件類型列表之內
bool isHas = ClassLibrary.RData.RString.StrIsIncUseSC(exp, config.ImgSafeType.Split(‘|'));
if (isHas)
{
string domainOutter = context.Request.UrlReferrer.Authority.ToLower(); //包含域名和端口
ArrayList arry = Common.Cache.GetDomainValid();//取系統定義的合法的域名綁定列表
isSafe = arry.Contains(domainOutter); //判斷當前請求的域名是否在合法列表之內
}
}
//下面就是不合法的時候的輸出了,如果有默認替代圖片則輸出,如果沒有就生成一個,格式為。gif
if (!isSafe)
{
Bitmap img = null;
Graphics g = null;
MemoryStream 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) && HttpContext.Current.Request.UrlReferrer.Host != "localhost")
{
HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath("~/jzdl.jpg"));
HttpContext.Current.Response.End();
}
}
新聞熱點
疑難解答
圖片精選