保护 ASP.NET 中的单个文件
我有一个场景,用户可以访问一次性网址。 当用户单击 URL 时,该用户将可以使用特定文件。
我在网站上有很多文件,但只希望该用户可以访问某些文件。
我虽然有关于生成经过身份验证的 cookie 并使用基于表单的身份验证并对某个文件夹应用权限,但我需要对单个文件进行授权。并且文件会不断变化。
只允许用户访问特定文件的最佳方法是什么? (我不会显示其他文件,但我仍然不希望其他文件在 URL 中输入时可用)
I have a scenario where a user will have access to a one-time-url.
When the user clicks on the URL, specific files will be available to that user.
I have many files on the site but would only like certain files to be accessible by that user.
I have though about generating an authenticated cookie and using forms based auth and applying permissions to a certain folder, but I need authorization on indiviual files. and the files will constintly be changing.
What would be the best way to give a user only access to specific files? (I won't display the other files, but I still do not want other files available if they are typed in the URL)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将创建一个 .ashx (处理程序文件),并将文件提供给用户(加载到内存中,然后通过将文件推送到内容流来写出内容)。这样,最终用户永远没有系统上实际文件的权限,但仍然可以访问它们。然后,您的代码可以控制每个文件可供用户使用的时间和时长。
I would create an .ashx (handler file) and have that serve the files to the user (load into memory and then write the contents out by pushing the file to the content stream). That way the end user never has permissions to the actual files on the system but can still access them. Your code can then control when and how long each file is available to a user.
我将提供有关实际文件检索的抽象。这样用户就永远看不到文件名。像 www.example.com/File.aspx?id=SOMERANDOMGUID
这样的 RANDOMGUID 可以引用后端的文件。
I would provide an abstraction around the actual file retrieval. That way the user never sees file name. Something like www.example.com/File.aspx?id=SOMERANDOMGUID
That RANDOMGUID could reference a file in the back end.
如果您有大量磁盘空间,实现此目的的一种方法是将文件复制到随机生成的文件夹,以便每个用户的用户文件的 URL 都是唯一的。
If you have lots of disk space, one way to accomplish this is to copy the files to a randomly-generated folder, so that the URL to a user's files is unique for each user.
我认为如果您的文件与 ID 相关联并且路径保存在数据库中会更容易。这样您就可以使用 ID 提取文件。
I think it would be easier if your files are associated with an ID and the path is kept in the database. This way you can pull the files using the ID.