一、简单写法,在Global.asax下的这个方法中添加如下信息
1 /// <summary> 2 /// 管道当中的第一个事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void Application_BeginRequest(object sender, EventArgs e) 7 { 8 //防盗链 9 if (Request.RawUrl.Contains("images/")) 10 { 11 if (Request.UrlReferrer==null || !IsSameDomain(Request.UrlReferrer,Request.Url)) 12 { 13 Response.ContentType = "image/jpeg"; 14 string path = Request.MapPath("/daplian.jpg"); 15 Response.WriteFile(path); 16 Response.End(); 17 } 18 } 19 } 20 /// <summary> 21 /// 判断域名是否相同 22 /// </summary> 23 /// <param name="u1"></param> 24 /// <param name="u2"></param> 25 /// <returns></returns> 26 bool IsSameDomain(Uri u1,Uri u2) 27 { 28 return Uri.Compare(u1, u2, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCulture) == 0 ? true : false; 29 }