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

发布于 2024-10-31 21:34:05 字数 114 浏览 1 评论 0原文

我的 ASP.NET 应用程序中有一个文件夹,其中包含只能由某种类型的连接用户(管理员帐户或允许的其他帐户)访问(下载)的文档文件,我

该怎么做?
有什么想法吗?
提前致谢。

i have a folder in my asp.net app conatining doc files that can be accessed only (dowwload) by a certain type of connected users(admin account or permitted other accounts)

how can i do that?
any ideas?
thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如歌彻婉言 2024-11-07 21:34:05

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

The App_Data folder in .NET is protected, and therefore ideal for this very purpose. I normally put sensitive files in here then have a page "ViewDoc.aspx" that performs the security checks and then sends the file to the user (using Response.Write).

硬不硬你别怂 2024-11-07 21:34:05

将敏感文件放在网站根目录之外,这样就无法通过 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" />

记住添加您的处理程序的文件类型并根据文件类型更改response.contenttype

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

Put sensitive files outside of web site root, so they can not be accessed by URL.

After that, use this HttpHandler (written in VB.NET) to serve files:

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

and register this handler in your web.config like this:

<httpHandlers>
        <add verb="*" path="secfile.axd" type="MyApp.FileHandler, MyApp" validate="false"/>
    </httpHandlers>

use like this:

<a href="secfile.axd?pic=sample.jpg" />

Remember adding your file types to handler and change response.contenttype by type of your file.

Using a handler is not the only way, you can use context.Response.TransmitFile(fileInfo.FullName) in your aspx file.

给妤﹃绝世温柔 2024-11-07 21:34:05

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

A simple way to do this is to NOT put these documents inside a folder of your ASP.NET app and instead, put it somewhere else in the file system that can't be accessed directly from the browser. Then programmatically, you can serve the file to the user if s/he's authorized to do so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文