禁止未登录用户访问后台和下载 以前我是用session方式来验证的,但是无法保证用户不登录就直接下载资源,想参考下各位大哥大姐是怎么做的,是否是webconfig配置之类的,能否给点好的代码? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用日志记录用户是否登录或session,cookie结合 使用流输出就不用担心了被看到下载链接了. 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(); } 只要你做了必要的验证,比如sessioon,cookies 不论用什么工具都不会知道你文件的真实地址。他们只能得到一个类似xxx.aspx?Id=123的地址,你在xxx.asp中根据数据库的记录查出文件地址,直接用FileStream和Response就可以了。 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文件夹里 把你的下载连接做成 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> <?xml version="1.0"?><configuration> <system.web> <authorization> <allow roles ="downgroup"/> //允许downgroup 角色下载 <deny users="*" />//这个根据限制级别设定 拒绝其他角色,或用户 </authorization> </system.web></configuration>http://www.mybuffet.cn response.redirect()不执行 类的内容怎么查? Coalesys.PanelBar 百思不得其解,一个奇怪的问题,关于自定义Web.config文件配置节的问题 关于酒店管理系统的问题 谁能帮我把这段C#代码改写成VB.NET的,谢谢.快没分了.给50吧~~~ 关于一个在TEXTBOX中,属性设成多行后作为一个文本输入框的问题??在线等!!!!!!!!!! DataGrid的问题!? 大家帮忙(在线等...) ASP.NET怎么发布程序?我写的东西移动到另一个目录就有问题了 客户端向服务器端提交方式有哪些? 大侠们帮忙看下这是什么问题
或session,cookie结合
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();
}
只要你做了必要的验证,比如sessioon,cookies 不论用什么工具都不会知道你文件的真实地址。他们只能得到一个类似xxx.aspx?Id=123的地址,你在xxx.asp中根据数据库的记录查出文件地址,直接用FileStream和Response就可以了。
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文件夹里
把后台aspx和download.aspx 放到 子文件夹中 ,在这个文件夹中 放web.config,写如下内容<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="?" />//这个根据限制级别设定
</authorization>
</system.web>
</configuration>
<configuration>
<system.web>
<authorization>
<allow roles ="downgroup"/> //允许downgroup 角色下载
<deny users="*" />//这个根据限制级别设定 拒绝其他角色,或用户
</authorization>
</system.web>
</configuration>
http://www.mybuffet.cn