[收藏] ASP及ASP.Net的防盗链实现

ASP版本: http://www.blueidea.com/tech/program/2004/1673.asp

如果我们知道一个静态文件的实际路径如: http://www.xx.com/download/51windows.pdf
如果服务器没有作特别的限制设置,我们就可以毫不费力的把它下载下来!
当网站提供51windows.pdf下载时,怎么样才能让下载者无法得到他的实际路径呢!
本文就来介绍如何使用Asp来隐藏文件的实际下载路径。

  我们在管理网站文件时,可以把扩展名一样的文件放在同一个目录下,起一个比较特别名字,
例如放pdf文件目录为the_pdf_file_s,把下面代码另存为down.asp,
他的网上路径为http://www.xx.com/down.asp
我们就可以用http://www.xx.com/down.asp?FileName=51windows.pdf来下载这个文件了,
而且下载者无法看到这个文件实际下载路径的!
在down.asp中我们还可以设置下载文件是否需要登陆,判断下载的来源页是否为外部网站,从而可以做到防止文件被盗链。

示例代码:

<% From_url = Cstr(Request.ServerVariables("HTTP_REFERER")) Serv_url = Cstr(Request.ServerVariables("SERVER_NAME")) if mid(From_url,8,len(Serv_url)) <> Serv_url then response.write "非法链接!" '防止盗链 response.end end if if Request.Cookies("Logined")="" then response.redirect "/login.asp" '需要登陆! end if Function GetFileName(longname)'/folder1/folder2/file.asp=>file.asp while instr(longname,"/") longname = right(longname,len(longname)-1) wend GetFileName = longname End Function Dim Stream Dim Contents Dim FileName Dim TrueFileName Dim FileExt Const adTypeBinary = 1 FileName = Request.QueryString("FileName") if FileName = "" Then Response.Write "无效文件名!" Response.End End if FileExt = Mid(FileName, InStrRev(FileName, ".") + 1) Select Case UCase(FileExt) Case "ASP", "ASA", "ASPX", "ASAX", "MDB" Response.Write "非法操作!" Response.End End Select Response.Clear if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then Response.ContentType = "image/*" '对图像文件不出现下载对话框 else Response.ContentType = "application/ms-download" end if Response.AddHeader "content-disposition", "attachment; filename=" & GetFileName(Request.QueryString("FileName")) Set Stream = server.CreateObject("ADODB.Stream") Stream.Type = adTypeBinary Stream.Open if lcase(right(FileName,3))="pdf" then '设置pdf类型文件目录 TrueFileName = "/the_pdf_file_s/"&FileName end if if lcase(right(FileName,3))="doc" then '设置DOC类型文件目录 TrueFileName = "/my_D_O_C_file/"&FileName end if if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then TrueFileName = "/all_images_/"&FileName '设置图像文件目录 end if Stream.LoadFromFile Server.MapPath(TrueFileName) While Not Stream.EOS Response.BinaryWrite Stream.Read(1024 * 64) Wend Stream.Close Set Stream = Nothing Response.Flush Response.End %>


Asp.Net版本: http://blog.joycode.com/liuhuimiao/archive/2004/04/04/18180.aspx

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

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”,然后确定即可。

 

5、  IE里输入http://193.100.100.56/TestWebSolution/WebApplication1/test.rar,会立即跳转到http://193.100.100.56/TestWebSolution/WebApplication1/WebForm1.aspx,然后按WebForm1.aspx的“下载”按钮就可以下载test.rar了。

 

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

l         http://www.9seek.com/news/show.aspx?id=745&cid=12

l         http://www.9seek.com/news/show.aspx?id=521&cid=12

l         http://www.9seek.com/news/show.aspx?id=520&cid=12

l         http://msdn.microsoft.com/asp.net/using/building/web/default.aspx?pull=/library/en-us/dnaspp/html/URLRewriting.asp




转载于:https://www.cnblogs.com/babyt/archive/2005/04/28/146656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值