我已经在IIS8上设置了一个dotnet core API,用于为(aurelia)应用程序打开上传文件夹。我使用以下方式打开对该文件夹的访问权限:
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(Configuration["ApiSettings:UploadsFolder"]),
RequestPath = new PathString("/uploads/"),
EnableDirectoryBrowsing = true
});
这个应用程序可以通过URL在应用程序中打开已上传的图片,非常正常。现在我想当直接在浏览器中输入URL时,阻止对这些图像的访问。我看到过C#的解决方案,但找不到dotnet core的解决方案。
我正在使用中间件来拦截发送到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);
}
有谁能告诉我阻止直接访问图像并仅允许特定应用加载图像的最佳方法是什么?