【问题标题】:File download in asp.net在 asp.net 中下载文件
【发布时间】:2011-06-15 05:16:08
【问题描述】:

我的网站上有一个链接。每当用户单击此链接时,都会在此操作中下载一个 pdf 文件。 但是如果用户知道文件链接,他/她直接在浏览器中添加文件地址,也可以下载文件,就会出现问题。 我想通过在浏览器地址栏中输入文件 url 来保护它被直接下载。 对此有任何建议。

问候

SYED

【问题讨论】:

    标签: asp.net file download


    【解决方案1】:

    嗯,

    • 您可以使用令牌(此“令牌”保存在 HTML 元素的输入 type='hidden' name='token' value='token value',供以后使用)
    • 检查引用者是否在您的表单页面中

    “令牌”,您检查开始下载 pdf 文件的时间。

    我不能在asp.net中做这个,但是你可以看看ASP.net。

    简单示例代码:

    <?
    $token = $_SESSION['token'] = md5(uniqid()); 
    echo "<form method='POST' action=""><input type='hidden' name='token' value='$token'>
          <input type='submit' value='Go!'>"; 
    
    if($_POST["token"] == $token) {
        //show link pdf file
    }else {
       die('Access denied');
    }
    
    ?>
    

    我没有说这是什么完全的安全性,说的是另一种选择。

    【讨论】:

      【解决方案2】:

      将文件放在网站文件夹之一中,并使用表单身份验证和位置属性从 web.config 将身份验证设置为该文件夹。用户需要登录才能下载 PDF。

      <authentication mode="Forms">
                  <forms defaultUrl="Default.aspx" loginUrl="~/adminlogin.aspx" slidingExpiration="true" timeout="30"></forms>
              </authentication>
      
              <location path="[PDF Folder]">
              <system.web>
                  <authorization>
                      <allow roles="[role]"/>
                      <deny users="*"/>
                  </authorization>
              </system.web>
          </location>
      
      This is only sample Get more details about above from net.
      

      【讨论】:

        猜你喜欢
        • 2012-01-25
        • 1970-01-01
        • 1970-01-01
        • 2010-11-12
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-18
        • 2021-12-10
        相关资源
        最近更新 更多