IHttpHandler 轻松实现图片防盗链
2008-01-21 13:43
507 查看
VS2008,C#
1. web.config配置
在config的<httpHandlers></httpHandlers>节点加入:
<add verb="*" path="*.gif,*.jpg,*.png" type="ImgProtector.BLL.SiteImgProtector"/>
verb:* 表示对所有http请求方式进行处理
path:*.gif,*.jpg,*.png,表示处理所有站点的gif/jgp/png文件,可以以逗号分割的方式继续往下加
type:处理这个请求的具体类,实现IHttpHandler接口
2. 实现IHttpHandler接口
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace ImgProtector.BLL {
public class SiteImgProtector : IHttpHandler {
IHttpHandler Members#region IHttpHandler Members
public bool IsReusable {
get {
return true;
}
}
public void ProcessRequest(HttpContext context) {
string path = context.Request.PhysicalPath;
string serverHost = context.Request.Url.Host;
Uri u = context.Request.UrlReferrer;
if (u == null || u.Host.ToLower() != serverHost.ToLower()) {
context.Response.WriteFile("~/Forbidden.gif");
}
else {
context.Response.WriteFile(path);
}
}
#endregion
}
}
1. web.config配置
在config的<httpHandlers></httpHandlers>节点加入:
<add verb="*" path="*.gif,*.jpg,*.png" type="ImgProtector.BLL.SiteImgProtector"/>
verb:* 表示对所有http请求方式进行处理
path:*.gif,*.jpg,*.png,表示处理所有站点的gif/jgp/png文件,可以以逗号分割的方式继续往下加
type:处理这个请求的具体类,实现IHttpHandler接口
2. 实现IHttpHandler接口
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace ImgProtector.BLL {
public class SiteImgProtector : IHttpHandler {
IHttpHandler Members#region IHttpHandler Members
public bool IsReusable {
get {
return true;
}
}
public void ProcessRequest(HttpContext context) {
string path = context.Request.PhysicalPath;
string serverHost = context.Request.Url.Host;
Uri u = context.Request.UrlReferrer;
if (u == null || u.Host.ToLower() != serverHost.ToLower()) {
context.Response.WriteFile("~/Forbidden.gif");
}
else {
context.Response.WriteFile(path);
}
}
#endregion
}
}
相关文章推荐
- ASP.NET MVC 利用IRouteHandler, IHttpHandler实现图片防盗链
- 使用HttpHandler隐藏图片真实地址,轻松实现防盗链
- 图片地址防盗链,通过IHttpHandler实现
- 图片地址防盗链,通过IHttpHandler实现
- ASP.NET MVC 利用IRouteHandler, IHttpHandler实现图片防盗链
- 图片地址防盗链,通过IHttpHandler实现
- 图片地址防盗链,通过IHttpHandler实现
- Apache服务器使用.htaccess实现图片防盗链教程
- 轻松实现将上传图片到数据库
- Http Handler 介绍 及通过IhttpHandler实现图片验证码
- Apache服务器使用.htaccess实现图片防盗链
- 用 Chukeh.dll 轻松实现图片的缩略图,旋转,缩放,剪切,拼合,水印等操作
- 轻松实现:垂直居中文字图片
- 利用Thumbnailator轻松实现图片缩放、旋转与加水印
- ASP.NET实现图片防盗链(转)
- 轻松实现图片上传并回显
- ASP.NET 实现简单的图片防盗链介绍
- 轻松实现Bootstrap图片轮播
- ASP.NET之图片防盗链的实现
- Apache服务器使用.htaccess实现图片防盗链