【发布时间】:2015-04-10 03:02:48
【问题描述】:
我试图限制匿名用户直接浏览我网站文件夹中的特定文件名(图像文件)。但是当我打开文件夹的“IIS Authentication”功能时,匿名用户和网站应用程序都无法访问图像文件。
如何拒绝匿名用户访问文件(例如,如果用户输入绝对 URL),但允许访问网站应用程序? (我认为也许“IP 地址和域限制”功能也可以使用,但无法使用)
我可以将图像文件移动到网站外部的文件夹中,但不确定如何在 .ImageUrl 属性中使用它。
...撞
编辑 - 解决方案(将以下内容放在 .aspx 页面中,并将 ImageUrl 属性设置为此页面,并使用任何所需的查询字符串参数):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Request.QueryString("FileType") IsNot Nothing) And (Request.QueryString("FileName") IsNot Nothing) Then
Try
' Read the file and convert it to Byte Array
Dim filePath As String = UrlXlat(Request.QueryString("FileType") & "\")
Dim fileName As String = Request.QueryString("FileName")
Dim contentType As String = "image/" & Path.GetExtension(fileName).Replace(".", "")
Dim fs As FileStream = New FileStream(filePath & fileName, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
br.Close()
fs.Close()
'Write the file to Reponse
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = contentType
Response.AddHeader("content-disposition", "attachment;filename=" & fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
Catch ex As Exception
response.write(ex):response.end
End Try
End If
End Sub
【问题讨论】:
标签: asp.net iis authorization relative-path restriction