您好,欢迎访问一九零五行业门户网

asp.net HttpHandler实现图片防盗链_javascript技巧

step.1:创建文件 customhandler.cs,代码如下:
复制代码 代码如下:
using system;
using system.web;
namespace customhandler{
public class jpghandler : ihttphandler{
public void processrequest(httpcontext context){
// 获取文件服务器端物理路径
string filename = context.server.mappath(context.request.filepath);
// 如果urlreferrer为空,则显示一张默认的禁止盗链的图片
if (context.request.urlreferrer.host == null){
context.response.contenttype = image/jpeg;
context.response.writefile(/error.jpg);
}else{
// 如果 urlreferrer中不包含自己站点主机域名,则显示一张默认的禁止盗链的图片
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 编译这个文件
复制代码 代码如下:
csc /t:library /r:system.web.dll customhandler.cs
step.3 将编译好的 customhandler.dll 拷贝到站点的 bin 目录下。
step.4 在web.config 中注册这个handler。
复制代码 代码如下:
ok,诸位可以按步骤自行测试一下,这里就不赘述了。
其它类似信息

推荐信息