asp.net HttpHandler實現(xiàn)圖片防盜鏈
更新時間:2009年11月09日 18:57:21 作者:
這個例子來自于《Maximizing ASP.NET Real World, Object-Oriented Development》一書, 需要的朋友可以參考下。
Step.1:創(chuàng)建文件 CustomHandler.cs,代碼如下:
using System;
using System.Web;
namespace CustomHandler{
public class JpgHandler : IHttpHandler{
public void ProcessRequest(HttpContext context){
// 獲取文件服務(wù)器端物理路徑
string FileName = context.Server.MapPath(context.Request.FilePath);
// 如果UrlReferrer為空,則顯示一張默認的禁止盜鏈的圖片
if (context.Request.UrlReferrer.Host == null){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}else{
// 如果 UrlReferrer中不包含自己站點主機域名,則顯示一張默認的禁止盜鏈的圖片
if (context.Request.UrlReferrer.Host.IndexOf("yourdomain.com") > 0){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile(FileName);
}else{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}
}
}
public bool IsReusable{
get{ return true; }
}
}
}
Step.2 編譯這個文件
csc /t:library /r:System.Web.dll CustomHandler.cs
Step.3 將編譯好的 CustomHandler.dll 拷貝到站點的 Bin 目錄下。
Step.4 在Web.Config 中注冊這個Handler。
<system.web>
<httpHandlers>
<add path="*.jpg" verb="*" type="CustomHandler.JpgHandler, CustomHandler" />
</httpHandlers>
</system.web>
OK,諸位可以按步驟自行測試一下,這里就不贅述了。
復(fù)制代碼 代碼如下:
using System;
using System.Web;
namespace CustomHandler{
public class JpgHandler : IHttpHandler{
public void ProcessRequest(HttpContext context){
// 獲取文件服務(wù)器端物理路徑
string FileName = context.Server.MapPath(context.Request.FilePath);
// 如果UrlReferrer為空,則顯示一張默認的禁止盜鏈的圖片
if (context.Request.UrlReferrer.Host == null){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}else{
// 如果 UrlReferrer中不包含自己站點主機域名,則顯示一張默認的禁止盜鏈的圖片
if (context.Request.UrlReferrer.Host.IndexOf("yourdomain.com") > 0){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile(FileName);
}else{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}
}
}
public bool IsReusable{
get{ return true; }
}
}
}
Step.2 編譯這個文件
復(fù)制代碼 代碼如下:
csc /t:library /r:System.Web.dll CustomHandler.cs
Step.3 將編譯好的 CustomHandler.dll 拷貝到站點的 Bin 目錄下。
Step.4 在Web.Config 中注冊這個Handler。
復(fù)制代碼 代碼如下:
<system.web>
<httpHandlers>
<add path="*.jpg" verb="*" type="CustomHandler.JpgHandler, CustomHandler" />
</httpHandlers>
</system.web>
OK,諸位可以按步驟自行測試一下,這里就不贅述了。
相關(guān)文章
bootstrap datetimepicker 日期插件在火狐下出現(xiàn)一條報錯信息的原因分析及解決辦法
日期插件 bootstrap-datetimepicker 在火狐下出現(xiàn)一條報錯信息:TypeError: (intermediate value).toString(…).split(…)[1] is undefined.什么原因?qū)е碌哪?,下面小編給大家分享解決思路,需要的朋友參考下2017-03-03
JS實現(xiàn)CheckBox復(fù)選框全選全不選功能
在網(wǎng)站的管理后臺應(yīng)用此功能居多,如一次性處理多個產(chǎn)品,或?qū)ξ恼碌膭h除對產(chǎn)品的下架等處理,一條一條的點顯然有一些麻煩,如果能每一行放一個checkbox,然后統(tǒng)一處理就好辦的多了,今天我就用簡單的篇幅來講解一下這個功能的實現(xiàn)原理和實現(xiàn)過程。2015-05-05
jQuery.extend()的實現(xiàn)方式詳解及實例
extend()函數(shù)是jQuery的基礎(chǔ)函數(shù)之一,作用是擴展現(xiàn)有的對象2013-06-06

