【发布时间】:2017-01-29 20:48:24
【问题描述】:
我已经设置了一个 dotnet 核心 API(在 IIS8 上),它为(aurelia)应用程序打开了一个上传文件夹。我已经像这样打开了对该文件夹的访问权限
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(Configuration["ApiSettings:UploadsFolder"]),
RequestPath = new PathString("/uploads/"),
EnableDirectoryBrowsing = true
});
这很好用,因为应用程序可以通过 URL 在应用程序中打开上传的图像。现在,当直接在浏览器中输入 URL 时,我想阻止对这些图像的访问。我见过 C# 解决方案,但找不到 dotnet 核心解决方案。
我正在使用中间件拦截发送到 api 的请求。但无法通过浏览器拦截特定的图像请求并阻止它们。唯一允许访问图像的应该是我在固定域上的 aurelia 应用程序。
public async Task Invoke(HttpContext context)
{
StringValues referer;
context.Request.Headers.TryGetValue("Referer", out referer);
// Referer will contain the full URL of the image when requested
// via the browser. When its requested differently it will contain
// the url of the requesting application
if (referer.Any() && referer.First().Contains("/uploads/"))
{
context.Response.StatusCode = 401;
return;
}
await _next.Invoke(context);
}
谁能告诉我最好的方法是阻止直接访问图像并只允许某个应用程序加载图像?
【问题讨论】:
-
我想在我的 .net 核心应用程序中做类似的事情。您将 Task Invoke 函数放在哪里以及从哪里调用它(如果有的话)。是在 Startup.cs 中吗?
标签: c# browser .net-core asp.net-core-webapi