比如:输入 http://localhost/Index/my.doc,来直接下载文件
怎样禁止这种行为?而让用户只能在应用程序中访问?
怎样禁止这种行为?而让用户只能在应用程序中访问?
解决方案 »
- Ms Chart或Dundas Chart的legend显示问题?
- push 推技术怎么在母版页里实现
- asp.net c# 在repeater中使用服务器控件
- 采用asp.net 的forms认证,采用身份验证票验证登录,怎么样能设置保存期为1年。1年之内不用登录直接进入后台
- 请做过asp.net连接sybase数据库的朋友指点下
- 求一正则表达式
- ajax.net Cookie 问题.鼓捣两天了,救命啊!
- |zyciis| 100分求教,现在要做一个图文报表,问一下大家是怎么来做漂亮的ASP.NET报表 有没有哪些比较好的控件
- 控制水晶报表中的子报表问题!
- 求验证IP地址和端口的正则正则正则正则正则正则正则正则正则正则正则正则正则正则正则正则
- asp.net+c# 如何实现页面定时刷新?
- 救命!快交付的系统突然发现的奇怪问题
if(strUrl.SubString(strUrl.Length-3,3).ToLower()=="doc")
{
..不能访问
}
一个是将文件放在服务器的虚拟目录外的文件夹里。ASP.NET的程序可以引用,外部不能访问。
另一个方法是使用httpHandlers方法,虽然麻烦一些,效率也要低一些,但是功能更强大,可以实现对某个文件对具体用户进行授权。具体思路为,在Web.Config里增加:
<httpHandlers>
<add verb="*" path="rar/*.rar" type="HandlerDownRAR.DownRARHandler,DownRAR"/>
</httpHandlers>
然后在IIS里映射文件扩展名。你可以自己写出你希望的类HandlerDownRAR.DownRARHandler(类文件名为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