【发布时间】:2021-04-26 19:44:19
【问题描述】:
我正在创建一个 asp.net 核心应用程序并使用 .Net5。我正在 www-root 文件夹之外上传文件。这是我的目录配置。
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(@$"{Configuration["AppConfiguration:PhysicalDirectoryBasePath"]}"),
RequestPath = new PathString("/app-data"),
EnableDirectoryBrowsing = false
});
我想保护该文件夹,以便没有公共用户可以访问它。我想在访问文件之前检查标题
header contains app-token then allow file to access otherwise not
我无法停止公共用户的文件访问。如何做到这一点?
【问题讨论】:
-
一种解决方案是从 startup.cs 文件中删除上述代码,然后通过控制器提供文件。但是有很多种文件,例如 html、js 和 images 。如何处理所有这些文件?
-
只需添加一个控制器动作,将文件发送到网络,
return File(path); -
你不认为它会从系统中暴露我的物理路径吗?
-
不,这会保护您的文件系统,您必须以某种方式识别文件,我不会将“路径”作为路由参数发送。
-
好的,知道了。这样我也可以检查上下文标题?对吗?
标签: asp.net-mvc asp.net-core .net-core .net-5