比如:输入 http://localhost/Index/my.doc,来直接下载文件
怎样禁止这种行为?而让用户只能在应用程序中访问?

解决方案 »

  1.   

    你可以把文件放到web站点的根目录外,然后通过页面代码实现下载就没有问题了。
      

  2.   

    string strUrl=Request.RowUrl.ToString();
    if(strUrl.SubString(strUrl.Length-3,3).ToLower()=="doc")
    {
       ..不能访问
    }
      

  3.   

    我用过两个方法。
    一个是将文件放在服务器的虚拟目录外的文件夹里。ASP.NET的程序可以引用,外部不能访问。
    另一个方法是使用httpHandlers方法,虽然麻烦一些,效率也要低一些,但是功能更强大,可以实现对某个文件对具体用户进行授权。具体思路为,在Web.Config里增加:
    <httpHandlers>
             <add verb="*" path="rar/*.rar" type="HandlerDownRAR.DownRARHandler,DownRAR"/>
    </httpHandlers>
    然后在IIS里映射文件扩展名。你可以自己写出你希望的类HandlerDownRAR.DownRARHandler(类文件名为DownRAR.vb),对所有的对该扩展名文件的访问,进行精确的授权验证或其它控制。
      

  4.   

    to:aspdotnet2005(红枫)你的类文件能否让参考一下?虽说我要用C#来写
      

  5.   

    上面写错了 是        bbla()
      

  6.   

    bbla() 
    能否再详细一点呢?
      

  7.   

    实际上就是在IIS中映射RAR文件,将它交由HandlerDownRAR.DownRARHandler类(DownRAR.vb)处理(以System.IO.Stream的方式向用户输出文件)。下面贴出DownRAR.vb文件的代码。授权及控制部分代码已经删除,请大家自行根据自己的需要在输出流之前添加控制代码。如果代码运行出错那就是错删了一些语句。' 命名此文件名为 DownRAR.vb
    ' 在DOS命令行下进入其所在目录下,执行以下命令: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\vbc /t:Library /r:System.Web.dll /r:System.dll DownRAR.vb
    ' 拷贝生成的文件 DownRAR.dll 到 bin 目录下
    ' 在IIS里进行RAR的映射设置
    ' 在Web.config里做以下配置:
    '<httpHandlers>
    '<add verb="*" path="rar/*.rar" type="HandlerDownRAR.DownRARHandler,DownRAR"/>
    '</httpHandlers>.Imports System
    Imports System.WebNamespace HandlerDownRAR    Public Class DownRARHandler
            Implements IHttpHandler
            Implements SessionState.IRequiresSessionState    '使用Session判断状态及授权用        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
                Dim Fname As String
                Fname = Request.PhysicalPath Dim iStream As System.IO.Stream ' Buffer to read 10K bytes in chunk:
    Dim buffer(10000) As Byte ' Length of the file:
    Dim length As Integer ' Total bytes to read:
    Dim dataToRead As Long ' Identify the file name.
    Dim filename As String = System.IO.Path.GetFileName(Fname) Try
    ' Open the file.
    iStream = New System.IO.FileStream(Fname, System.IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read) ' Total bytes to read:
    dataToRead = iStream.Length Response.ContentType = "application/octet-stream"
    Response.AddHeader("Content-Disposition", "attachment; filename=" & filename) ' Read the bytes.
    While dataToRead > 0
    ' Verify that the client is connected.
    If Response.IsClientConnected Then
    ' Read the data in buffer
    length = iStream.Read(buffer, 0, 10000) ' Write the data to the current output stream.
    Response.OutputStream.Write(buffer, 0, length) ' Flush the data to the HTML output.
    Response.Flush() ReDim buffer(10000) ' Clear the buffer
    dataToRead = dataToRead - length
    Else
    'prevent infinite loop if user disconnects
    dataToRead = -1
    End If
    End While Catch ex As Exception
    context.Server.Transfer("../404.html")
    Response.End()
    Finally
    If Not iStream Is Nothing Then
    ' Close the file.
    iStream.Close()
    End If
    End Try        End Sub        Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
                Get
                    Return True
                End Get
            End Property
        End ClassEnd Namespace