asp.net
所謂盜鏈就是指其他網站把我們網站的檔案連結帖到他們站上,這樣白白佔用我們的頻寬。訪問對於網站盜鏈行為,是非常不道德的。要實現防盜鏈,我們就得在IIS處理URL時攔截。
效果圖:
未加防盜鏈之前:hm是我的機器名,用http://hm/myweb/default.aspx和http://localhost/myweb/default訪問結果一樣。
這幅圖片是任人宰割的。
加了防盜鏈之後雖然還是同一個網站但是http://hm/myweb/default.aspx已經不能訪問那副花卉圖片,被以下圖片替代:
加了防盜鏈之後用localhost還是正常的!http://localhost/myweb/default訪問結果一樣。
原理:
其實hm是我的機器,但是由於伺服器網域名稱是localhost所以即使是同一個網站也不能訪問,所以就更別說
www.其他網站網域名稱.com這樣的網站偷取我們的資源。關鍵就是IIS對所有的請求進行過濾看看是不是本站網域名稱的。
全部代碼:
Web.Config
<?xml version="1.0"?>
<!--
注意: 除了手動編輯此檔案以外,您還可以使用
Web 管理工具來配置應用程式的設定。可以使用 Visual Studio 中的
“網站”->“Asp.Net 配置”選項。
設定和注釋的完整列表在
machine.config.comments 中,該檔案通常位於
\Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<httpHandlers>
<add verb="*" path="*.jpg" type="myhandler,App_Code"/>
</httpHandlers>
<!--
設定 compilation debug="true" 將偵錯符號插入
已編譯的頁面中。但由於這會
影響效能,因此只在開發過程中將此值
設定為 true。
-->
<compilation debug="true"/>
<!--
通過 <authentication> 節可以配置 ASP.NET 使用的
安全身分識別驗證模式,
以標識傳入的使用者。
-->
<authentication mode="Windows"/>
<!--
如果在執行請求的過程中出現未處理的錯誤,
則通過 <customErrors> 節可以配置相應的處理步驟。具體說來,
開發人員通過該節可以配置
要顯示的 html 錯誤頁
以代替錯誤堆疊追蹤。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>
myhandler.cs 建立myhandler.cs 類時系統提示你要放入App_Code中
using System;
using System.Web;
/// <summary>
/// myhandler 的摘要說明
/// </summary>
public class myhandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string FileName = context.Server.MapPath(context.Request.FilePath);
if (context.Request.UrlReferrer.Host == null)
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("~/no.gif");//被替換圖片
}
else
{
if (context.Request.UrlReferrer.Host.IndexOf("localhost") > -1)//這裡是你的網域名稱
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile(FileName);
}
else
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("~/no.gif");
}
}
}
public bool IsReusable
{
get { return true; }
}
public myhandler()
{
}
}
Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>清清月兒http://blog.csdn.net/21aspnet</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="http://www.pushad.com/Info/pic130.jpg" /></div>
</form>
</body>
</html>
http://www.pushad.com/Info/pic130.jpg
no.gif
IIS的配置:
配置應用程式擴充:添加一個.jpg的擴充!
注意:在本地的context.Request.UrlReferrer.Host就是localhost,
我開始以為http://localhost/A/和http://localhost/B/是不同的context.Request.UrlReferrer.Host,那就是大錯特錯。http://localhost/A/和http://localhost/B/的context.Request.UrlReferrer.Host都是localhost,所以測試一個用localhost,所以,本地測試用機器名例如我的是hm測試即可。經過處理後用機器名訪問就不行,雖然還是同一個網站,同一個檔案,此處請多注意。
下面是怎麼防rar檔案不從主站下載:方法和圖片類似,不過下載我們強迫他們到我們網站。
1、 首先建立一個類庫項目ClassLibrary1:
using System;
using System.Web; // 引用System.Web組件
public class MyHandler : IHttpHandler
{
public MyHandler()
{
}
#region IHttpHandler 成員
public void ProcessRequest(HttpContext context)
{
// 跳轉到WebForm1.aspx,由WebForm1.aspx輸出rar檔案
HttpResponse response = context.Response;
response.Redirect("../manage/downloads.aspx");
}
public bool IsReusable
{
get
{
// TODO: 添加 MyHandler.IsReusable getter 實現
return true;
}
}
#endregion
}
2、 在設定檔Web.config檔案節點裡增加如下節點:
<httpHandlers>
<add verb="*" path="*.rar" type="myhandler,App_Code"/>
</httpHandlers>
3、 在WebForm1.aspx裡增加一個文本為“下載”的Button,其Click事件如下:
注意別忘記了using System.IO;
private void Button1_Click(object sender, System.EventArgs e)
{
FileInfo file = new System.IO.FileInfo(Server.MapPath("1.rar"));
Response.Clear();
Response.AddHeader("Content-Disposition", "filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
string fileExtension = file.Extension;
// 根據檔案尾碼指定檔案的Mime類型
switch (fileExtension)
{
case ".mp3":
Response.ContentType = "audio/mpeg3";
break;
case "mpeg":
Response.ContentType = "video/mpeg";
break;
case "jpg":
Response.ContentType = "image/jpeg";
break;
case "........等等":
Response.ContentType = "....";
break;
default:
Response.ContentType = "application/octet-stream";
break;
}
Response.WriteFile(file.FullName);
Response.End();
}
4、 最後一步就是在IIS裡增加一個應用程式擴充。在“預設網站”->“屬性”->“主目錄”->“配置”。在彈出的“應用程式配置”視窗裡按“添加”,在彈出的“添加/編輯應用程式副檔名映射”視窗裡“可執行檔”選擇C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,在副檔名裡輸入“.rar”,然後確定即可。
5、 在IE裡輸入http://localhost/web/1.rar,會立即跳轉到http://localhost/web/WebForm1.aspx,然後按WebForm1.aspx的“下載”按鈕就可以下載1.rar了。