亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

asp.net HttpHandler實(shí)現(xiàn)圖片防盜鏈

 更新時(shí)間:2009年11月09日 18:57:21   作者:  
這個(gè)例子來(lái)自于《Maximizing ASP.NET Real World, Object-Oriented Development》一書, 需要的朋友可以參考下。
Step.1:創(chuàng)建文件 CustomHandler.cs,代碼如下:
復(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為空,則顯示一張默認(rèn)的禁止盜鏈的圖片
if (context.Request.UrlReferrer.Host == null){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}else{
// 如果 UrlReferrer中不包含自己站點(diǎn)主機(jī)域名,則顯示一張默認(rèn)的禁止盜鏈的圖片
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 編譯這個(gè)文件
復(fù)制代碼 代碼如下:

csc /t:library /r:System.Web.dll CustomHandler.cs

Step.3 將編譯好的 CustomHandler.dll 拷貝到站點(diǎn)的 Bin 目錄下。
Step.4 在Web.Config 中注冊(cè)這個(gè)Handler。
復(fù)制代碼 代碼如下:

<system.web>
<httpHandlers>
<add path="*.jpg" verb="*" type="CustomHandler.JpgHandler, CustomHandler" />
</httpHandlers>
</system.web>

OK,諸位可以按步驟自行測(cè)試一下,這里就不贅述了。

相關(guān)文章

最新評(píng)論