画像の盗難防止
5053 ワード
1 public class ProtectImgHandler:IHttpHandler
2 {
3 public bool IsReusable
4 {
5 get { return false; }
6 }
7
8 public void ProcessRequest(HttpContext context)
9 {
10 context.Request.ContentType = "image/JPEG";
11 //
12 string FileName = context.Server.MapPath(context.Request.FilePath);
13 // UrlReferrer ,
14 if (context.Request.UrlReferrer.Host == null)
15 {
16 context.Response.WriteFile("~/img/error.jpg");
17 }
18 else {
19 // UrlReferrer ,
20 if (context.Request.UrlReferrer.Host.IndexOf("localhost") > -1)
21 {
22 context.Response.WriteFile(FileName);
23 }
24 else {
25 context.Response.WriteFile("~/img/error.jpg");
26 }
27 }
28 }
29
30 // Web.config
31 <httpHandlers>
32 <add verb="*" path="*.jpg" type="Picture.ProtectImgHandler"/>
33 </httpHandlers>