ASP.NET通用檔案與防盜鏈

來源:互聯網
上載者:User

標籤:physical   rgs   server   檔案   require   擴充   log   name   request方法   

添加Web→全域應用程式類,注 檔案名稱不要改 Global.asax
通用檔案是對Web應用聲明周期的一個事件響應的地方,將Web應用啟動時初始化的一些代碼寫到
Application_Start中,比如後面講的Log4Net的初始化等。應用關閉的時候Application_End調用
當一個Session啟動的時候Session_Start被調用,Session結 (使用者主動退出或者逾時結 )
Session_End被調用。當一個使用者請求來的時候Application_BeginRequest方法被調用當應用中出
現未捕獲異常,Application_Error被調用(常考,ASP.Net中的錯誤處理機制),
用HttpContext.Current.Server.GetLastError()獲得異常資訊,然後用 Log4Net記錄到日誌中。

 

1.通過全域配製實現圖片的防盜鏈

 

//jztu.jpg 禁止盜鏈的圖片protected void Application_BeginRequest(object sender,EventArgs e){    if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".jpg")&&        HttpContext.Current.Request.UrlReferrer.Host != "www.gao.com")    {            HttpContext.Current.Response.WriteFile(HttpContext.Current.        Server.MapPath("~/jztu.jpg"));        HttpContex.Current.Response.End();    }else{        // 驗證通過輸出使用者請求的檔案        HttpContext.Current.Response.TransmitFile(HttpContext.Current.Request.PhysicalPath);    }}

 

屏蔽指定的IP地址

 

protected void Application_BeginRequest(object sender,EventArgs e){    if (HttpContext.Current.Request.UserHostAddress == "127.0.0.1")    {            HttpContext.Current.Response.Write("該IP以被屏蔽!");        HttpContext.Current.Response.End();    }else{        // 驗證通過輸出使用者請求的檔案        HttpContext.Current.Response.TransmitFile(HttpContext.Current.Request.PhysicalPath);    }}

IIS發布注意事項:發布到IIS上需要設定一下IIS的 處理常式映射->添加指令碼映射->請求路徑(*.jpg)->可執行檔(ASP.NET的處理常式),一次只能添加一個副檔名

還有一種更簡便的方法就是在Web.config 裡面設定 在 <system.webServer> 節點裡面添加如下:

<!-- 設定IIS對指定擴充的處理常式為ASP.NET --><handlers>    <add name="防盜鏈 png" path="*.png" verb="*" modules="IsapiModule" scriptProcessor="%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness32" /></handlers>

 

通過自訂 HttpHandler 實現盜鏈

 1. 自訂的 HttpHandler

public class Protect : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {           if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".jpg")&&        HttpContext.Current.Request.UrlReferrer.Host != "www.gao.com")       {            HttpContext.Current.Response.WriteFile(HttpContext.Current.        Server.MapPath("~/jztu.jpg"));        HttpContex.Current.Response.End();        }else{            // 驗證通過輸出使用者請求的檔案            HttpContext.Current.Response.TransmitFile(HttpContext.Current.Request.PhysicalPath);        }        }        public bool IsReusable        {            get            {                return false;            }        }    }

2. 在Web.config還要在 <system.web> 下配置模組

      <!-- 指定某些副檔名的程式給指定的程式解析,可指定多個 -->      <httpHandlers>        <add verb="*" path="*.png,*.jpg,*.gif,*.png,*.mp3,*.wma,*.lrc" type="FlyMusic.Web.Com.Protect,FlyMusic.Web"/>      </httpHandlers>

3. 發布到IIS時和上面一樣

 

ASP.NET通用檔案與防盜鏈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.