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

推荐订阅源

爱范儿
爱范儿
P
Palo Alto Networks Blog
月光博客
月光博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
aimingoo的专栏
aimingoo的专栏
腾讯CDC
T
Threatpost
D
DataBreaches.Net
Vercel News
Vercel News
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
C
Cybersecurity and Infrastructure Security Agency CISA
Forbes - Security
Forbes - Security
U
Unit 42
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
O
OpenAI News
量子位
TaoSecurity Blog
TaoSecurity Blog
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Visual Studio Blog
Recorded Future
Recorded Future
云风的 BLOG
云风的 BLOG
Security Archives - TechRepublic
Security Archives - TechRepublic
The Last Watchdog
The Last Watchdog
S
Security Affairs
Attack and Defense Labs
Attack and Defense Labs
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
小众软件
小众软件
S
SegmentFault 最新的问题
www.infosecurity-magazine.com
www.infosecurity-magazine.com
W
WeLiveSecurity
AI
AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
I
Intezer
Know Your Adversary
Know Your Adversary
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
博客园_首页
NISL@THU
NISL@THU
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - Chep

招聘 dotNet c# asp.net 、Windows Mobile 、iPhone 开发人员,工作地上海 iblogs博客程序1.0版 求一问题的解决方法 .TEXT 修改 二 - Chep 微软.net精简框架常见问题及回答(中文版) 对.text的修改(1) 博客RSS 使用完全手册 主流 Blog 程序 添加 免责声明 Gif 文件格式译解 Visual Studio.Net鲜为人知的技巧 DotNet 网上资源 gif 文档 .net学习:显示/播放Gif动画 在.NET中实现彩色光标和自定义光标 创建基于 Microsoft .NET Framework 精简版的动画控件 Windows Mobile-一个新时代的开始 关于跨站验证 关于更新blog
[转载]防止盗链下载问题
Chep · 2005-05-28 · via 博客园 - Chep

转自:http://www.cnblogs.com/Daview/archive/2004/04/24/7436.aspx

防止盗链下载问题

   经常在网络上四处载东西,有时碰到直接拷贝一个类似http://193.100.100.56/TestWebSolution/WebApplication1/test.rar地址准备下载test.rar文件时,却被告知没有登录或者直接跳转到其他页面的情况,然后等登录后直接下载该文件。要实现上面情况,在.NET世界里是比较容易的。

1、  首先创建一个类库项目ClassLibrary1,实现如下(点这里查看):

using System;

using System.Web;    // 引用System.Web组件

namespace ClassLibrary1

{

    public class MyHandler : IHttpHandler

    {

        public MyHandler()

        {

        }

        #region IHttpHandler 成员

        public void ProcessRequest(HttpContext context)

        {

            // 跳转到WebForm1.aspx,由WebForm1.aspx输出rar文件

            HttpResponse response = context.Response;

    response.Redirect("http://193.100.100.56/TestWebSolution/WebApplication1/WebForm1.aspx");

        }

        public bool IsReusable

        {

            get

            {

                // TODO:  添加 MyHandler.IsReusable getter 实现

                return true;

            }

        }

        #endregion

    }

}

2、  创建测试用的Web项目WebApplication1。在配置文件Web.config文件

节点里增加如下节点:

  <httpHandlers>

               <add verb="*" path="*.rar" type="ClassLibrary1.MyHandler, ClassLibrary1" />

httpHandlers>

3、  在WebForm1.aspx里增加一个文本为“下载”的Button,其Click事件如下(点这里查看):

FileInfo file = new System.IO.FileInfo(@"G:\WebCenter\TestWebSolution\WebApplication1\test.rar");

// FileInfo 类在 System.IO 命名空间里

              Response.Clear();

              Response.AddHeader("Content-Disposition", "filename=" + file.Name);

              Response.AddHeader("Content-Length", file.Length.ToString());

              string fileExtension = file.Extension;

              // 根据文件后缀指定文件的Mime类型

              switch (fileExtension)

              {

                   case ".mp3":

                       Response.ContentType = "audio/mpeg3";

                       break;

                   case "mpeg":

                       Response.ContentType = "video/mpeg";

                       break;

                   case "jpg":

                       Response.ContentType = "image/jpeg";

                       break;

                   case "........等等":

                       Response.ContentType = "....";

                       break;

                   default:

                       Response.ContentType = "application/octet-stream";

                       break;

              }

              Response.WriteFile(file.FullName);

              Response.End();

4、  最后一步就是在IIS里增加一个应用程序扩展。在“默认网站”->“属性”->“主目录”->“配置”。在弹出的“应用程序配置”窗口里按“添加”,在弹出的“添加/编辑应用程序扩展名映射”窗口里“可执行文件”选择C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll,在扩展名里输入“.rar”,然后确定即可。

6、  当然,这里只按例子给个思路,完全可以再根据自身情况扩展。下面有几个参考的资源文章: