惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Project Zero
Project Zero
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
The Register - Security
The Register - Security
C
Check Point Blog
Attack and Defense Labs
Attack and Defense Labs
L
LangChain Blog
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
Recorded Future
Recorded Future
GbyAI
GbyAI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Y
Y Combinator Blog
量子位
A
About on SuperTechFans
I
Intezer
T
Threat Research - Cisco Blogs
MongoDB | Blog
MongoDB | Blog
U
Unit 42
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 聂微东
NISL@THU
NISL@THU
The Hacker News
The Hacker News
G
Google Developers Blog
F
Full Disclosure
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Privacy & Cybersecurity Law Blog
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
T
Tenable Blog
Know Your Adversary
Know Your Adversary
Stack Overflow Blog
Stack Overflow Blog
K
Kaspersky official blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
C
Cybersecurity and Infrastructure Security Agency CISA
Martin Fowler
Martin Fowler
Schneier on Security
Schneier on Security

博客园 - 站在天空下的猪

SqlBulkCopy批量转移数据备注 根据文本框联动下拉框(jquery 插件) - 站在天空下的猪 - 博客园 关于用FOMR提交编码的问题 jquery 图片预览插件 - 站在天空下的猪 - 博客园 自己写的一个jquery模板引擎(json比较好用) Document 对象的常用方法 今天发现梅花雨日历控件蛮好用的哟,腾讯都在用 身份证对应县及县的行政区划代码 【SQLSERVER】存储过程基础 终于解决了一个AjaxPro无刷新的问题了,可以引用在短信方面滴asp.net 2.0 昨天很失败 一个简单的FileUpload处理逻辑 C# 判断是否为数字 DataBinder.Eval总结 如何把HTML语句直接保存到数据库 如何取得IP/用户名等信息 “ConnectionString 属性尚未初始化”的另类解决办法 asp.net常用函数表 C#中计算两个时间的差
asp.net 2.0中实现防盗链
站在天空下的猪 · 2007-05-16 · via 博客园 - 站在天空下的猪

 今天看书时发现一个实现asp.net 2.0中实现防止盗链的方法,其实际的原理就是利用了IIS中HttpHandler模块来处理的。因为比如
平时用IIS只是处理如asp,aspx等文件,没处理如iis,jpg等图片的,下面简单小结下

1 建一个网站(vs.net 2005),然后添加一个Handler.ashx处理文件,处理HTTP请求,代码如下
  <%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        //判断是否是本地引用,如果是则返回给客户端正确的图片
        //这里的判断就是用到了http请求中所记录的页信息
        //如果是网站,可将“localhost”修改为网站地址
        if (context.Request.UrlReferrer.Host == "localhost")
        {
            //设置客户端缓冲中文件过期时间为0,即立即过期。
            context.Response.Expires = 0;
            //清空服务器端为此会话开辟的输出缓存
            context.Response.Clear();
            //获得文件类型
            context.Response.ContentType = "image/jpg";
            //将请求文件写入到输出缓存中
            context.Response.WriteFile(context.Request.PhysicalPath);
            //将输出缓存中的信息传送到客户端
            context.Response.End();
        }
        //如果不是本地引用,则属于盗链引用,返回给客户端错误的图片
        else
        {
            //设置客户端缓冲中文件过期时间为0,即立即过期。
            context.Response.Expires = 0;
            //清空服务器端为此会话开辟的输出缓存
            context.Response.Clear();
            //获得文件类型
            context.Response.ContentType = "image/jpg";
            //将特殊的报告错误的图片文件写入到输出缓存中
            context.Response.WriteFile(context.Request.PhysicalApplicationPath + "error.jpg");
            //将输出缓存中的信息传送到客户端
            context.Response.End();
        }
    }
    public bool IsReusable
    {
        get
        {
            return true;
        }
    }

}
 然后同样建立一个Handler.cs文件,放在app_code目录下,其内容就是上面的handler.aschx了,
2  在web.config中配置如下
    <httpHandlers>
   <add verb = "*" path = "*.jpg"   type="Handler" />
  </httpHandlers>

3  在IIS里处理
    在IIS里的默认网站的“配置”里,在“应用程序映射”里添加映射
其中映射的可执行文件为  “vs.net2005安装路径\aspnet_isapi.dll",扩展名为".jpg",这样就可以了