如何保护用户上传的文件

发布于 2024-12-10 20:02:02 字数 441 浏览 1 评论 0原文

我有 asp mvc 项目。在其中我添加了目录结构:

...
  UserUploads
    User_1
      Images
        Original
        Thumb
        Display
    User_2
    User_n
...

如何使此文件夹结构对用户不可见?我不希望未经授权的用户通过输入网址看到此图像。每个用户都有自己的图像,只有某个用户的“朋友”用户才能看到它的图像。我检查了 Facebook 图像,图像地址如下:

http://a4.sphotos.ak.fbcdn.net/hphotos-ak-ash4/296040_2530953916384_1329592446_32898884_1499197273_n.jpg

那么,使用户上传文件安全的最佳实践是什么?

I have asp mvc project. And in it I have added directory structure:

...
  UserUploads
    User_1
      Images
        Original
        Thumb
        Display
    User_2
    User_n
...

How to make this folder structure invisible to users? I don't want that unauthorized users see this images by typing url. Each user has it's own images and only user that is 'friend' of some user can see it's images. I inspected facebook images and image address is like:

http://a4.sphotos.ak.fbcdn.net/hphotos-ak-ash4/296040_2530953916384_1329592446_32898884_1499197273_n.jpg

So what is the best practice to make user uploaded files secure?

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

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

发布评论

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

评论(2

后知后觉 2024-12-17 20:02:02

好吧,您可以使用两种方法:

  1. 真正的安全性 - 完全限制对文件夹结构的访问,并仅使用一些 HttpHandler (或 MVC 操作)来为它们提供服务(在评估访问权限后,通过 TransferFile 或流编写器将它们写入响应) ..)。从性能的角度来看,这是非常糟糕的 - 没有缓存,每个图片的整个服务器端处理等。

  2. “混淆”安全性 - 简单地生成无法猜测的文件名(Guid 是很好的候选者),所以没有人如果无法访问将其文件名生成为 HTML 的页面,则无法直接访问它们。当然,如果一个“朋友”访问它,然后将文件 URL 提供给未经授权的人,但他可以轻松地将文件本身发送给他......这种方式的性能非常好 - 文件是可缓存的(通过 IIS 和客户端) ),您的应用程序不必处理图像等请求。

Well, you can use 2 approaches :

  1. real security - restrict access to the folder structure entirely, and use only some HttpHandler (or MVC action) to serve them (after evaluating access rights, writing them to response via TransferFile or stream writer..). This is very bad from performance point of view - no caching, entire server side processing for each and every picture, etc..

  2. "obfuscation" security - simply generate filenames that cant be guessed (Guid is good candidate), so nobody without access to page that will generate their filename into HTML cant access them directly. Sure, if one "friend" access it, and then give the file URL to unauthorized person, but he can as easily send him the file itself... This way the performance is very good - files are cacheable (by IIS and by client), your app doesnt have to process requests for images, etc.

≈。彩虹 2024-12-17 20:02:02

只需在 web.config 文件中限制该 UserUploadsFolder 的访问即可:

<location path="UserUploads">
    <system.web>
        <authorization>
            <deny users="*"/>
        </authorization>
    </system.web>
</location>

Just restrict access in web.config file for that UserUploadsFolder:

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