Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
限制对asp.net文档文件的访问_Asp.net_Security_Web Config_Global Asax - Fatal编程技术网

限制对asp.net文档文件的访问

限制对asp.net文档文件的访问,asp.net,security,web-config,global-asax,Asp.net,Security,Web Config,Global Asax,我的asp.net应用程序中有一个包含文档文件的文件夹,该文件夹只能由特定类型的连接用户(管理员帐户或允许的其他帐户)访问(dowwload) 我该怎么做? 有什么想法吗? 提前感谢。一个简单的方法是不要将这些文档放在ASP.NET应用程序的文件夹中,而是放在文件系统中无法直接从浏览器访问的其他位置。然后,通过编程,您可以在用户获得授权的情况下将文件提供给用户。在.NET中的App_数据文件夹受到保护,因此非常适合用于此目的。我通常将敏感文件放在这里,然后有一个页面“ViewDoc.aspx”执

我的asp.net应用程序中有一个包含文档文件的文件夹,该文件夹只能由特定类型的连接用户(管理员帐户或允许的其他帐户)访问(dowwload)

我该怎么做?
有什么想法吗?

提前感谢。

一个简单的方法是不要将这些文档放在ASP.NET应用程序的文件夹中,而是放在文件系统中无法直接从浏览器访问的其他位置。然后,通过编程,您可以在用户获得授权的情况下将文件提供给用户。

在.NET中的App_数据文件夹受到保护,因此非常适合用于此目的。我通常将敏感文件放在这里,然后有一个页面“ViewDoc.aspx”执行安全检查,然后将文件发送给用户(使用Response.Write)。

将敏感文件放在网站根目录之外,这样它们就不能通过URL访问

之后,使用这个
HttpHandler
(用VB.NET编写)来提供文件:

Public NotInheritable Class FileHandler
    Implements IHttpHandler

    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
        If Not String.IsNullOrEmpty(context.Request.QueryString("FileName")) Then
            Dim fileName As String = context.Request.QueryString("FileName")

            Try


                Dim filesPath As String = "D:\TheFiles\"

                Dim fileInfo As New IO.FileInfo(filesPath & fileName)

                If fileInfo.Exists Then

                    Dim fileExt As String = fileInfo.Extension.Remove(0, 1).ToUpperInvariant


                    If fileExt = "JPG" Then
                        context.Response.ContentType = "image/jpeg"
                    Else
                        context.Response.ContentType = "image/" & fileExt
                    End If

                    context.Response.TransmitFile(fileInfo.FullName)

                End If

            Catch ex As Exception
            End Try

        End If
    End Sub

End Class
并在web.config中注册此处理程序,如下所示:

<httpHandlers>
        <add verb="*" path="secfile.axd" type="MyApp.FileHandler, MyApp" validate="false"/>
    </httpHandlers>
<a href="secfile.axd?pic=sample.jpg" />

这样使用:

<httpHandlers>
        <add verb="*" path="secfile.axd" type="MyApp.FileHandler, MyApp" validate="false"/>
    </httpHandlers>
<a href="secfile.axd?pic=sample.jpg" />

记住将文件类型添加到处理程序中,并按文件类型更改
response.contenttype


使用处理程序不是唯一的方法,你可以在你的aspx文件中使用
context.Response.TransmitFile(fileInfo.FullName)

谢谢Johnny。我们如何在asp.net中提供文档文件。谢谢@James。我如何使用Response.write with.doc(.docx)文件。你能给我一个指向工作示例的链接吗,这里有几个更详细的链接:。基本步骤是设置Response.ContentType=“application/msword”,使用某物获取字节,将字节数组传递给Response.Write(…)并调用Response.End()。在这种情况下,secfile.axd是什么?在web.config.me中我想保护文件夹中的所有files.doc。/DocFiles/