using
System;
System.Web;
///
<summary>
ImageProtect 的摘要說明
</summary>
public
class
ImageProtect : IHttpHandler
{
ImageProtect()
{
//
TODO: 在此處添加構造函數邏輯
}
void
ProcessRequest(HttpContext context)
判斷是否是本地引用,如果是則傳回給用戶端正确的圖檔,這裡的判斷就是用到了前面所述的http請求中所記路的參考頁資訊
此處使用了端口,僅用于
if
(context.Request.UrlReferrer.Host
==
"
localhost
&&
context.Request.UrlReferrer.Port
6999
)
{
設定用戶端緩沖中檔案過期時間為0,即立即過期。
context.Response.Expires
=
;
清空伺服器端為此會話開辟的輸出緩存
context.Response.Clear();
獲得檔案類型
context.Response.ContentType
image/jpeg
;
将請求檔案寫入到伺服器端為此會話開辟的輸出緩存中
context.Response.WriteFile(context.Request.PhysicalPath);
将伺服器端為此會話開辟的輸出緩存中的資訊傳送到用戶端
context.Response.End();
}
else
如果不是本地引用,則屬于盜鍊引用,傳回給用戶端錯誤的圖檔
将特殊的報告錯誤的圖檔檔案寫入到伺服器端為此會話開辟的輸出緩存中
context.Response.WriteFile(
~/images/error.jpg
);
context.Response.End();
bool
IsReusable
get
return
false
;
}
Web.Config
<
httpHandlers
>
add verb
*
path
*.jpg
type
ImageProtect
/>
</
2
.
#region
IHttpHandler 成員
IHttpHandler.IsReusable
{
true
; }
IHttpHandler.ProcessRequest(HttpContext context)
string
FileName
context.Server.MapPath(context.Request.FilePath);
null
)
image/JPEG
/no.jpg
);
(context.Request.UrlReferrer.Host.IndexOf(
mydomain.com
)
{
context.Response.ContentType
context.Response.WriteFile(FileName);
}
context.Response.WriteFile(
no/jpg
#endregion
編譯成dll。
JpgHandler, MyDll
在IIS網站配置中把JPG映射到aspnet_isapi.dll