以前我是用session方式来验证的,但是无法保证用户不登录就直接下载资源,想参考下各位大哥大姐是怎么做的,是否是webconfig配置之类的,能否给点好的代码?

解决方案 »

  1.   

    使用日志记录用户是否登录
    或session,cookie结合
      

  2.   

    使用流输出就不用担心了被看到下载链接了.
                FileInfo Fi = new FileInfo(filePath);
                if (Fi.Exists)
                {
                    FileStream fs = new FileStream(filePath, FileMode.Open);
                    byte[] bytes = new byte[(int)fs.Length];
                    fs.Read(bytes, 0, bytes.Length);
                    fs.Close();
                    Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(dt.Rows[0]["Name"].ToString(), System.Text.Encoding.UTF8));
                    Response.BinaryWrite(bytes);
                    Response.Flush();
                    Response.End();
                }
      

  3.   


    只要你做了必要的验证,比如sessioon,cookies  不论用什么工具都不会知道你文件的真实地址。他们只能得到一个类似xxx.aspx?Id=123的地址,你在xxx.asp中根据数据库的记录查出文件地址,直接用FileStream和Response就可以了。
      

  4.   

          FileInfo Fi = new FileInfo(filePath);
                if (Fi.Exists)
                {
                    FileStream fs = new FileStream(filePath, FileMode.Open);
                    byte[] bytes = new byte[(int)fs.Length];
                    fs.Read(bytes, 0, bytes.Length);
                    fs.Close();
                    Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(dt.Rows[0]["Name"].ToString(), System.Text.Encoding.UTF8));
                    Response.BinaryWrite(bytes);
                    Response.Flush();
                    Response.End(); 
                }
    把你的毛片放在App_Data文件夹里
      

  5.   

    把你的下载连接做成  download.aspx?file=1.rar 这样的连接,在 download.aspx中重定向 到 1.rar
    把后台aspx和download.aspx 放到 子文件夹中 ,在这个文件夹中 放web.config,写如下内容<?xml version="1.0"?>
    <configuration>
      <system.web>  
        <authorization>
          <deny users="?" />//这个根据限制级别设定 
        </authorization>
      </system.web>
    </configuration>
      

  6.   

    <?xml version="1.0"?>
    <configuration>
      <system.web> 
        <authorization>
          <allow roles ="downgroup"/> //允许downgroup 角色下载
          <deny users="*" />//这个根据限制级别设定 拒绝其他角色,或用户
        </authorization>
      </system.web>
    </configuration>
    http://www.mybuffet.cn