当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net中利用ashx实现图片防盗链的原理分析

ASP.NET
ASP.NET在上传文件时对文件类型的高级判断的代码
JQuery运用ajax注册用户实例(后台asp.net)
Asp.net与SQLserver一起打包部署安装图文教程
asp.net 上传下载输出二进制流实现代码
asp.net(C#)解析Json的类代码
asp.net 截取字符串代码
asp.net ubb使用代码
asp.net XML文件操作实现代码
asp.net利用HttpModule实现防sql注入
ASP.NET(C#)中操作SQLite数据库实例
asp.net(c#)ref,out ,params的区别
asp.net(C#)防sql注入组件的实现代码
asp.net FCKeditor自定义非空验证
Asp.net TreeView来构建用户选择输入的方法 推荐
asp.net(C#)函数对象参数传递的问题
Asp.net中的GridView导出遇到的两个问题和解决方法
asp.Net 中获取一周第一天,一月第一天等实现代码
asp.net MaxLengthValidator 最大长度验证控件代码
C# 通用文件上传类
asp.net 自定义控件实现无刷新上传图片,立即显示缩略图,保存图片缩略图

ASP.NET 中的 asp.net中利用ashx实现图片防盗链的原理分析


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-11   浏览: 139 ::
收藏到网摘: n/a

盗链的危害我就不说了,网上有很多。下面是asp.net下利用ashx的防盗链原理分析
直接分析盗链原理:看下面用httpwatch截获的http发送的数据
GET /upload/tech/20091011/20091011143830_5737034557ef5b8c02c0e46513b98f90.ashx?img=/upload/tech/20091011/20091011143829_42a0e188f5033bc65bf8d78622277c4e.gif HTTP/1.1
Accept: */*
Referer: http://www.ruanchen.com/"codetitle">复制代码 代码如下:

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("/upload/tech/20091011/20091011143829_1f50893f80d6830d62765ffad7721742.gif"));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}


表示如果来源不为空,并且来源的服务器和当前服务器一致,那就表示是正常访问,非盗链。正常访问文件内容。
否则就是盗链,返回网站LOGO。
你甚至可以做成随机返回正确的图片,随机返回错误图片,或者定时返回正确图片,定时返回错误图片。
然后就是图片的使用了,这时使用图片就不是直接<input type="image" src="/upload/tech/20091011/20091011143829_42a0e188f5033bc65bf8d78622277c4e.gif" />了,而是<input type="image" src="/upload/tech/20091011/20091011143830_5737034557ef5b8c02c0e46513b98f90.ashx?img=/upload/tech/20091011/20091011143829_42a0e188f5033bc65bf8d78622277c4e.gif" />,就是说通过img,ashx来读取图片。别人盗链的话要用下面代码:<input type="image" src="http://www.ruanchen.com/" />。
赶紧给自己的网站加上防盗链吧!

评论 (0) All

登陆 还没注册?