Asp.net网站使用HttpHandler实现图片防盗链功能
2011-10-28 23:46
1026 查看
第一步:创建PicHandler.cs文件,代码如下:
using System.Web;
namespace MyHttpModule
{
public class PicHandler:IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string strFile = context.Server.MapPath(context.Request.FilePath);
if(context.Request.UrlReferrer==null)
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}
else
{
if(context.Request.UrlReferrer.Host.IndexOf("youdomain.com")>-1)
{
context.Response.ContentType = "imae/jpeg";
context.Response.WriteFile(strFile);
}
else
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}
}
}
/// <summary>
/// 获取一个值,该值指示其他请求是否可以使用IHttpHander实例。也就是后续的Http请求是不是可以继续使用实现了该接口的类的实例
/// </summary>
public bool IsReusable
{
get { return true; }
}
}
}
第二步:编译文件成DLL;
csc /t:library /r:System.Web.dll PicHandler.cs
第三步:将编译妇的DLL拷贝到Bin目录下;
第四步:在Web.config中注册这个Handler;
<httpHandlers>
<add path="*.jpg" verb="*" type="MyHttpHandler.PicHandler,MyHttpHandler"/>
</httpHandlers>第五步:在IIS中配置应用程序扩展(jpg)
C:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll
using System.Web;
namespace MyHttpModule
{
public class PicHandler:IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string strFile = context.Server.MapPath(context.Request.FilePath);
if(context.Request.UrlReferrer==null)
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}
else
{
if(context.Request.UrlReferrer.Host.IndexOf("youdomain.com")>-1)
{
context.Response.ContentType = "imae/jpeg";
context.Response.WriteFile(strFile);
}
else
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}
}
}
/// <summary>
/// 获取一个值,该值指示其他请求是否可以使用IHttpHander实例。也就是后续的Http请求是不是可以继续使用实现了该接口的类的实例
/// </summary>
public bool IsReusable
{
get { return true; }
}
}
}
第二步:编译文件成DLL;
csc /t:library /r:System.Web.dll PicHandler.cs
第三步:将编译妇的DLL拷贝到Bin目录下;
第四步:在Web.config中注册这个Handler;
<httpHandlers>
<add path="*.jpg" verb="*" type="MyHttpHandler.PicHandler,MyHttpHandler"/>
</httpHandlers>第五步:在IIS中配置应用程序扩展(jpg)
C:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll
相关文章推荐
- ASP.net 使用HttpHandler实现图片防盗链
- asp.net HttpHandler实现图片防盗链
- 在asp.net中使用HttpHandler为网站图片添加水印
- 在asp.net中使用HttpHandler为网站图片添加水印
- asp.net下结合HttpHandler实现图片防盗链
- asp.net下结合HttpHandler实现图片防盗链
- 使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender实现自动完成功能(上) 【转自http://dflying.cnblogs.com】
- asp.net继承IHttpHandler接口实现给网站图片添加水印功能实例
- 使用HttpHandler隐藏图片真实地址,轻松实现防盗链
- 在 ASP.NET 中使用 HTTPHandler 实现 Front Controller
- ASP.NET 防盗链的实现[HttpHandler]
- 在asp.net中使用jQuery实现类似QQ网站的图片切割效果
- ASP.NET 防盗链的实现[HttpHandler]
- MSDN: 在 ASP.NET 中使用 HTTPHandler 实现 Front Controller
- ASP.NET 防盗链的实现[HttpHandler]
- 在asp.Net中使用fileuplod控件实现上传图片的功能
- Asp.Net HttpHandler 实现验证码功能
- 使用HttpHandler实现图片防盗链
- 在asp.Net中使用fileuplod控件实现上传图片的功能
- ASP.NET 防盗链的实现[HttpHandler]