积极答复者
windows server 2003 iis6环境下如何防止服务器资源被他人读取(连接)?

问题
-
对asp.net很痴迷,一个从0开始学习的新人求教!希望各位能解决。问题如下。
在服务器wwwroot目录下放了个网站。但站内图片、flash等非逻辑资源被其他网站连接引用,俗称“盗链”,占用我服务器资源。请问如何解决这个问题,我有个解决思路,首先,asp.net在windows中是以一种身份运行(使用访问服务器文件),那么将图片、CSS、JS等资源放在另外的地方,即非网站目录下,可能是另一硬盘目录,在windows层面上拒绝外来身份访问这个特定文件(或目录),我想这应该是可以办到的,但苦于不知如何设置,又感无处求教,煞是着急!
或者各位有和正统的解决之道,不妨赐教,在下感谢不尽!
web项目目录大致如下:
wwwroot
my website(我的网站位置)
app_code
javascript
css
image
defalut.aspx
.....
other website
答案
-
建议你的解决办法有三个:
一、使用实现 IHttpHandler 接口的类处理客户端请求,并对来自站外的请求予以拒绝(推荐)。
下面的类实现的 IHttpHandler 接口并对请求的图片进行处理:public class ProtectFilesHandlerReferrer : IHttpHandler { public ProtectFilesHandlerReferrer() { } #region IHttpHandler 成员 bool IHttpHandler.IsReusable { get { return true; } } void IHttpHandler.ProcessRequest(HttpContext context) { string FileName = context.Server.MapPath(context.Request.FilePath); if (context.Request.UrlReferrer.Host == null) { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/no.jpg"); } else { //假设您的站点是 mydomain.com if (context.Request.UrlReferrer.Host.IndexOf("mydomain.com") > 0) //如果来自您自己的站点,则输出所请求的文件 { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(FileName); } else { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("no/jpg"); } } } #endregion }
此时,假设您所需要保护的目录是 ~/protectedfiles/,而且需要保护的文件类型是 .jpg,则你需要在站点的 Web.Config 文件中 Configuration 节做如下配置:
<location path="protectedfiles"> <system.web> <httpHandlers> <add verb="*" path="*.jpg" type="ProtectedFilesHandlerReferrer"/> </httpHandlers> </system.web> </location>
二、像你所说的将需要保护的资源放在用于 Web 发布的以外的位置,并使用文件IO类进行访问。但这样由于资源与你的网站位置不一致,容易在日志部署或做其他应用时带来不必要的麻烦,因此并不建议使用。
三、使用 Membership 对匿名请求进行验证,操作较为复杂,不推荐。- 已编辑 陈计节 2009年12月25日 1:50 原回复有不完善之处
- 已标记为答案 Andrew_ZhuModerator 2009年12月31日 2:23
全部回复
-
建议你的解决办法有三个:
一、使用实现 IHttpHandler 接口的类处理客户端请求,并对来自站外的请求予以拒绝(推荐)。
下面的类实现的 IHttpHandler 接口并对请求的图片进行处理:public class ProtectFilesHandlerReferrer : IHttpHandler { public ProtectFilesHandlerReferrer() { } #region IHttpHandler 成员 bool IHttpHandler.IsReusable { get { return true; } } void IHttpHandler.ProcessRequest(HttpContext context) { string FileName = context.Server.MapPath(context.Request.FilePath); if (context.Request.UrlReferrer.Host == null) { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/no.jpg"); } else { //假设您的站点是 mydomain.com if (context.Request.UrlReferrer.Host.IndexOf("mydomain.com") > 0) //如果来自您自己的站点,则输出所请求的文件 { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(FileName); } else { context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("no/jpg"); } } } #endregion }
此时,假设您所需要保护的目录是 ~/protectedfiles/,而且需要保护的文件类型是 .jpg,则你需要在站点的 Web.Config 文件中 Configuration 节做如下配置:
<location path="protectedfiles"> <system.web> <httpHandlers> <add verb="*" path="*.jpg" type="ProtectedFilesHandlerReferrer"/> </httpHandlers> </system.web> </location>
二、像你所说的将需要保护的资源放在用于 Web 发布的以外的位置,并使用文件IO类进行访问。但这样由于资源与你的网站位置不一致,容易在日志部署或做其他应用时带来不必要的麻烦,因此并不建议使用。
三、使用 Membership 对匿名请求进行验证,操作较为复杂,不推荐。- 已编辑 陈计节 2009年12月25日 1:50 原回复有不完善之处
- 已标记为答案 Andrew_ZhuModerator 2009年12月31日 2:23