如何仅向授权用户提供静态文件? [英] How do I serve static files only to authorized users?
问题描述
我有一个Excel电子表格的集合,我想在我的ASP.NET 5 Web应用程序中仅向授权用户提供.
I have a collection of Excel spreadsheets that I'd like to serve in my ASP.NET 5 webapp only to authorized users.
- 我应将文件存储在哪里?我假设使用wwwroot(例如wwwroot/files).
- 如果在wwwroot中,如何只允许授权用户访问? (我想将它们作为来自控制器的[Authorize] FileResult提供服务,但这仍然使文件保持打开状态,可以通过我相信的URL直接访问.)
- 如何通过控制器中的FileResult操作引用wwwroot中的位置?
非常感谢!
推荐答案
是的,它们应该放在wwwroot
中.当前没有内置的方法来保护wwwroot
目录.但是创建一个中间件模块来完成它非常简单.有一个易于遵循的教程此处.
Yes, they should go in wwwroot
. Currently there is no built-in way to secure wwwroot
directories. But creating a middleware module to accomplish it is pretty straightforward. There is an easy to follow tutorial here.
如果您不熟悉中间件的开发,我会发布一个GitHub项目,该项目展示了如何通过三个简单步骤来创建中间件.您可以在此处下载项目.
If you're not familiar with developing middleware, I posted a GitHub project that shows how to create middleware in three easy steps. You can download the project here.
您不需要控制器即可访问静态文件.
You don't need a controller to access static files.
这篇关于如何仅向授权用户提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!