有没有办法隐藏URL但它仍然有效?

时间:2014-07-10 18:02:26

标签: c# asp.net

我已经看过URLRewriting和重定向,我们大多相信这不是可行的方法。

我所拥有的是一个Web表单,它接收一个搜索字符串,然后将结果输出为链接到Web共享上托管的不同PDF文件。这些文件根本不是本地或Web表单的一部分。当您单击指向pdf的链接时,您将获得指向pdf的直接Web链接,例如:

http://www.mywebsite.com/portfolios/employees/evaluations/test1.pdf

如果用户获得该链接并退格为:

http://www.mywebsite.com/portfolios/employees

他们可能有权访问所有其他记录。该示例有点不好,因为我无法对手头的页面应用显式权限。所以我想知道是否有可能重写

http://www.mywebsite.com/portfolios/employees/evaluations/test1.pdf

类似

http://www.mywbesite.com/test1.pdf

并且它仍然有效吗?

2 个答案:

答案 0 :(得分:0)

为您的网站添加通用处理程序,我们称之为FileRetriever.ashx。这将允许我们使用一些逻辑来确保用户有权访问他们请求的文件。这假设您的用户已登录该站点(否则您将如何知道他们是否被允许访问该文件?)

public void ProcessRequest(HttpContext context)
    {
    if(String.IsNullOrEmpty(context.Request.QueryString["file"]))
        {
        //return error status code telling them to provide a file
        context.Response.End();
        }

    string requestedfile=context.Request.QueryString["file"];
    if(!File.Exists(Server.MapPath(requestedfile)))
         {
         //write error status code telling them the file doesn't exist. Probably a 404 error.
         context.Response.End();
         }

    if(HasAccess(context.User.Identity.Name, requestedfile))
       {
       //write the file to the response
       context.Response.ContentType= "";//replace with MIME type of requested file
       context.Response.WriteFile(context.Server.MapPath(requestedfile));
       context.Response.End();
       }
    else
       {
        //return error status code telling them they're unauthorized
        context.Response.End();
        }
    }

public static bool HasAccess(string username, string file)
    {
    //if user has access to the file, return true. Else return false. This might involve database lookups etc
    }

真正的魔力将发生在HasAccess函数中,您必须执行检查以确保他们有权访问该文件。所有其他的东西只是处理常见的事情,如忘记指定文件或指定无效文件。

用户然后通过请求此网址mysite.com/FileRetriever.ashx?file=path/to/document/document.pdf

来访问文件

答案 1 :(得分:-1)

你永远不应该强调永远将敏感文件暴露给网络。不知道url不是安全/身份验证方法。您希望保护的所有文件都应位于IIS / Web之外,并且需要由您的应用程序检索和提供。应用程序将根据某种级别的授权控制对文件的访问。

在AT& T等许多大公司中出现这种情况的原因是,一个用户需要通过尝试不同的网址组合来强制您网站上的所有链接并获取所有员工信息。不要走这条路。