asp.net中利用ashx實現圖片防盜鏈代碼
直接分析盜鏈原理:看下面用httpwatch截獲的http發送的數據
GET /Img.ashx?img=svn_work.gif HTTP/1.1
Accept: */*
Referer: /
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; CLR 1.1.4322; CLR 2.0.50727; CLR 3.0.04506.648; CLR 3.5.21022; CIBA)
Host:
Connection: Keep-Alive
該數據包表示請求/Img.ashx?img=svn_work.gif文件。我們可以看到Referer表示上一頁請求頁面地址,也就是文件來源。Host表示當前請求的主機地址。
下面是一個盜鏈的數據包
GET /Img.ashx?img=svn_work.gif HTTP/1.1
Accept: */*
Referer:
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; CLR 1.1.4322; CLR 2.0.50727; CLR 3.0.04506.648; CLR 3.5.21022; CIBA)
Host:
Connection: Keep-Alive
我們可以看到,上面兩個數據,表示對於同一個文件:/Img.ashx?img=svn_work.gif的'請求過程,這裏的不同就是Referer,也就是都是請求同一個文件,但是請求的來源是不同的。因此我們可以在程序裏判斷是否是來源於當前服務器,來判斷是否是盜鏈。明白原理以後,實現防盜鏈就非常簡單了。下面以圖片防盜鏈來實現一個演示。中添加一個文件,然後後臺代碼如下:
複製代碼 代碼如下:
using System;
using ections;
using ;
using ;
using ices;
using ocols;
namespace GetImage
{
///
/// $codebehindclassname$ 的摘要說明
///
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = cProfile1_1)]
public class Img : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
entType = "image/jpg";
if (eferrer != null && ls(, StringComparison.InvariantCultureIgnoreCase))
context.Response.WriteFile(context.Server.MapPath("~/" + context.Request.QueryString["img"]));
else
context.Response.WriteFile(context.Server.MapPath("~/logo.gif"));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
表示如果來源不爲空,並且來源的服務器和當前服務器一致,那就表示是正常訪問,非盜鏈。正常訪問文件內容。
否則就是盜鏈,返回網站LOGO。
你甚至可以做成隨機返回正確的圖片,隨機返回錯誤圖片,或者定時返回正確圖片,定時返回錯誤圖片。
然後就是圖片的使用了,這時使用圖片就不是直接了,而是,就是說通過img,ashx來讀取圖片。別人盜鏈的話要用代碼。
相關文章
-
防盜鏈接ASP函數實現代碼
簡單介紹下功能吧:使用了ASP的一個對象ServerVariables(服務器環境變量),通過這個環境變量可以獲取到真正的下載地址再通過一些簡單的內置函數破壞其真正地址的完整性。達到欺騙下載軟件的功能。現附上源碼提供大家學習。 -
關於ASP.NET使用JavaScript顯示信息提示窗口實現原理及代碼
關於使用JavaScript顯示信息提示窗口實現原理及代碼在使用JavaScript顯示信息窗口,你可下從的博客,下載一個DLL,放在站點的目錄。下載地址創建一個aspx頁面,然後在的做幾個動作,一是引用命名空間,實例化對象。然後在Pag -
MS SQL Server Management Studio Express安裝圖文具體步驟
Microsoft sql server Management Studio Express (SSMSE) 是一種免費、易用的圖形管理工具,用於管理 SQL Server 2005 Express Edition 和具有高級服務的 SQL Server 2005 Express Edition。SSMSE 還可以管理任何版本 -
Don’t Take the Age As an Excuse爲題目的作文
When I was small, I fell in love with tennis, so I like to watch tennis match so much. Today, the tennis players have extended their career time, before, most of them will retire around 30 years old, -
The truth about money and happiness高中英語作文
it is an oft repeated aiom that money cannot buy happiness. while this is certainly true, poverty will not buy happiness either. some people become very wealthy, yet struggle to enjoy their lives. on -
How to Enhance the Sense of Happiness英語四級作文
How to Enhance the Sense of Happiness1. 人們對於如何提升幸福感有不同看法2. 幸福感提升不僅包含物質層面,也指 範文:The sense of happiness is of great significance. As a vital factor of life, it brin -
WEALTH AND HAPPINESS高中英語作文
There is no same opinion among people as to the view of wealth. Some people may think that wealth is the most important and powerful thing in life, while some other people believe that there are many -
Is a Test of Spoken English Necessary英語四級作文
在日常學習、工作或生活中,大家都有寫作文的經歷,對作文很是熟悉吧,作文可分爲國小作文、中學作文、大學作文(論文)。相信寫作文是一個讓許多人都頭痛的問題,下面是小編爲大家收集的溫哥華街頭的愛作文,僅供參考,希望能夠幫助 -
用canvas就可以實現圖片的濾鏡轉化代碼實現
導語:canvas的getImageData可以獲取canvas內圖像的沒一個像素點的顏色值獲取,而且可以改變,下面是小編給大家提供的.用canvas就可以實現圖片的濾鏡轉化代碼實現,大家可以參考閱讀,更多詳情請關注應屆畢業生考試網。 使 -
國中英語作文My Strengths and Weaknesses
My Strengths and Weaknesses我的優缺點It is important to know our good and bad points because this knowledge will help us to improve ourself. We should examine ourselves and lea who we are.對於我們來說