我正在为 开发应用程序网上购书
我在付款后提供下载 PDF 格式的书,所以我想从直接下载中保护我的 PDF。
问题:
1) 我怎么可能下载 PDF 时隐藏文件夹 URL
2) 的任何安全代码不允许用户直接访问文件夹或 PDF
3) 当点击下载它将直接下载PDF而不是在浏览器中打开然后下载
最佳答案
有一百万种方法可以解决这个问题,所以我只是从臀部射击并抛出一些可能对你有帮助的想法。
推荐
首先,您不希望在应用程序中显式公开任何文件或文件夹。让您的应用程序为您服务,如果您甚至想直接为它们服务。您可以考虑很多事情:
潜在的工作流程可能是这样的:
如果您确实选择为用户提供明确下载文件的链接,请确保您不公开文件本身,而是提供一个端点来处理确定访问权限并最终像这样提供文件:
// You could consider writing a custom attribute that would store which files a given
// user had access to (via claims, etc.) but make sure the endpoint requires authentication
[Authorize]
public FileResult DownloadFile(Guid fileId)
{
// Here you could explicitly check in your database to ensure the user had access
// to the requested file, otherwise revoke the request
if (CanAccessFile(context.UserId, fileId))
{
// If they can access the file, then serve it from the appropriate location
return File(...);
}
}
同样,您还可以支持以下场景:只需访问链接即可允许用户通过传入请求的文件和 token 来下载文件:
// You wouldn't necessarily need authentication here because the token and
// requested file should be enough
public FileResult DownloadFileWithToken(Guid downloadToken, Guid fileId)
{
// Here you would just check your database to ensure that the token was
// valid for the specific file and if so, allow the user to download it
}
你的问题
1) How could I hide folder URL at the time of download PDF?
不要直接公开文件,以便用户可以访问它。
2) Any security code which not allow user directly access folder or PDF?
再次 - 不允许直接访问任何文件夹或文件。如果您在付款后提供这些文件,那么您没有理由在网站/应用程序上明确公开它们。
3) When click on download it will directly download PDF instead of open in browser then download?
您可以通过 HTML
download
属性完成此行为(无论您选择如何处理此过程):<a href="path" download>Download</a>
关于c# - 从 mvc5 中直接下载 pdf 保护并隐藏 url 和文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59367599/