您的位置:首页 > 编程语言 > ASP

asp.net中利用ashx实现图片防盗链

2009-12-21 12:28 816 查看
摘要: 文中给出了防止图片盗链的一种基于asp.net的方法。如果来源不为空,并且来源的服务器
和当前服务器一致,那就表示是正常访问,非盗链,正常访问文件内容。否则就是盗链。
盗链的危害我就不说了,网上有很多。

直接分析盗链原理:看下面用httpwatch截获的http发送的数据

GET /Img.ashx?img=svn_work.gif HTTP/1.1
Accept: */*
Referer: http://www.svnhost.cn/ Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA)
Host: www.svnhost.cn
Connection: Keep-Alive


该数据包表示请求http://www.svnhost.cn/Img.ashx?img=svn_work.gif文件。
我们可以看到Referer表示上一页请求页面地址,也就是文件来源。Host表示当前请求的主机地址。
下面是一个盗链的数据包

GET /Img.ashx?img=svn_work.gif HTTP/1.1
Accept: */*
Referer: http://745.cc/ Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA)
Host: www.svnhost.cn
Connection: Keep-Alive

我们可以看到,上面两个数据,表示对于同一个文件:
http://www.svnhost.cn /Img.ashx?img=svn_work.gif的请求过程,这里的不同就是Referer,也就是都是请求

同一个文件,但是请求的来源是不同的。 因此我们可以在程序里判断是否是来源于当前服务器,来判断是

否是盗链。明白原理以后,实现防盗链就非常简单了。下面以图片防盗链来实现一个演示。 ASP.NET中添

加一个img.ashx文件,然后后台代码如下:



using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace GetImage
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Img : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpg";
if (context.Request.UrlReferrer != null && context.Request.UrlReferrer.Host.Equals(context.Request.Url.Host, 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;
}
}
}
}
using System; using System.Collections; using System.Data; using System.Web; using System.Web.Services; using System.Web.Services.Protocols;  namespace GetImage {     /// <summary>     /// $codebehindclassname$ 的摘要说明     /// </summary>     [WebService(Namespace = "http://tempuri.org/")]     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]     public class Img : IHttpHandler     {          public void ProcessRequest(HttpContext context)         {             context.Response.ContentType = "image/jpg";             if (context.Request.UrlReferrer != null && context.Request.UrlReferrer.Host.Equals(context.Request.Url.Host, 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。你甚至可以做成随机返回正确的图片,随机返回错误图片,或者定

时返回正确图片,定时返回错误图片。

然后就是图片的使用了,这时使用图片就不是直接<input type="image" src="svn_work.gif" />了,而是

<input type="image" src="/Img.ashx?img=svn_work.gif" />,就是说通过img,ashx来读取图片。

别人盗链的话要用下面代码:<input type="image" src="http://www.svnhost.cn/Img.ashx?img=svn_work.gif" />。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: