我正在使用ASP.NET 4.0 Framework。我有一个包含10个PDF文件的目录,即pdf1,pdf2 .... pdf10。点击按钮我正在使用Response.Redirect&传递Pdf文件路径以便在浏览器中打开它。但是,这使用户能够使用此URL查看PDF文件夹的路径(url),他可以直接打开任何其他pdf。如何阻止他直接从网址
访问PDF答案 0 :(得分:8)
使用Request.ServerVariables["HTTP_REFERER"]
这将告诉您请求的来源。如果不在您的网站上,请采取适当的措施。
e.g。
if(Request.ServerVariables["HTTP_REFERER"].ToLower().IndexOf("mysite.com") == -1){
// Not from my site
Response.Redirect("NotAllowed.aspx");
}
答案 1 :(得分:1)
此link可以帮助您阻止他直接从网址访问PDF。
答案 2 :(得分:1)
在Global.asax.cs中使用此代码并将[NoDirectAccess]调用到所有控制器
//Prevent direct URL access: Call [NoDirectAccess] to all controllers to block
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class NoDirectAccessAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.UrlReferrer == null ||
filterContext.HttpContext.Request.Url.Host != filterContext.HttpContext.Request.UrlReferrer.Host)
{
filterContext.Result = new RedirectToRouteResult(new
RouteValueDictionary(new { controller = "Home", action = "Login", area = "" }));
}
}
}
答案 3 :(得分:0)
您需要添加安全图层。如果您正在使用MVC,那么实现可能会更简单,因为您将在控制器操作中执行授权。但是,对于经典ASP,您可能需要实现自定义处理程序。
答案 4 :(得分:0)
没有简单的解决方法。您可以根据服务器日期/时间设计某种滚动代码,该日期/时间必须是查询字符串的一部分,并在页面加载中检查它的正确性,如果您使其足够复杂/长,那么人们将无法手动输入。