demoshop

NEWS

demo, trying to be the best.

站內搜尋載入中...

透過IHttpHandler讓檔案必須登入才可下載

  • 4250
  • 0

雖然asp.net有內建的驗證機制,當你進入到頁面的時候如果沒有登入會要求您先行登入,但是如果我們網站中有放置【文字檔】、【壓縮檔】、等等不是asp.net相關的檔案,這些的檔案只要使用者知道位子以後,就可以直接輸入網址來達到下載的目的,如果你也需要避免類似情況,那就看看這篇文章吧。

     

       aspx的檔案在ASP.NET的驗證機制下,如果輸入demo.aspx,系統會自動導向到我們指定的登入畫面,當登入驗證完成後,會再次導向回剛剛想要瀏覽的網頁demo.aspx。而這機制,都是透過.NET Framework的ISAPI aspnet_isapi.dll來處理的。因此我們要利用aspnet_isapi.dll的處理,幫我們對於我們指定的副檔名也做出相同的驗證。因此必須處理三件事情。

    1. 撰寫IHttpHandler處理這些檔案類型
    2. 設定Web.Config來註冊IHttpHandler,並且設定該資料夾Deny Users="?"
    3. 在IIS中註冊這些副檔名由【aspnet_isapi.dll】處理(這樣才能啟動IHttpHandler)

    第一件事當然要把你的驗證機制先設定起來,後面才有搞頭阿

    再來我們再我們的網站中建立一個files的資料夾(放在這的都必須驗證)

    然後建立一個類別檔(class)名稱取為verifyFile.cs然後將下方code完整的貼上去

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
     
    ///  
    /// verifyFile 的摘要描述
    ///  
    public class verifyFire : IHttpHandler
    {
     
        public verifyFire()
        {
            //
            // TODO: 在此加入建構函式的程式碼
            //
        }
        bool IHttpHandler.IsReusable
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }
     
        public void ProcessRequest(HttpContext context)
        {
            string FileName = context.Request.FilePath;
            string[] tmpS = FileName.Split(new Char[] { '.' });
            string FileExten = tmpS[tmpS.Rank].ToLower();
            bool GetContentType = false;
            switch (FileExten)
            {
                case "txt":
                    context.Response.ContentType = "text/plain";
                    GetContentType = true;
                    break;
                case "doc":
                    context.Response.ContentType = "application/msword";
                    GetContentType = true;
                    break;
                case "xls":
                    context.Response.ContentType = "application/ms-excel";
                    GetContentType = true;
                    break;
                case "ppt":
                    context.Response.ContentType = "application/vnd.ms-powerpoint";
                    GetContentType = true;
                    break;
                case "pdf":
                    context.Response.ContentType = "application/pdf";
                    GetContentType = true;
                    break;
                case "zip":
                    context.Response.ContentType = "application/x-zip-compressed";
                    GetContentType = true;
                    break;
                case "gif":
                    context.Response.ContentType = "image/gif";
                    GetContentType = true;
                    break;
     
                case "tif":
                    context.Response.ContentType = "image/tiff";
                    GetContentType = true;
                    break;
     
                case "jpg":
                    context.Response.ContentType = "image/jpeg";
                    GetContentType = true;
                    break;
     
            }
            if (GetContentType)
            {
                context.Response.TransmitFile(context.Request.FilePath);
            }
            //context.Response.Write(FileExten)
            else
            {
                //context.Response.Write(FileExten)
                context.Response.Write("未設定檔案格式【" + FileExten + "】!!");
            }
        }
    } 

    ♥小提醒:如果您建立類別檔的時候不是使用範例的命名,請記得要改阿

    然後我們對者剛剛建立出來的files資料夾按右鍵,選擇加入新項目,我們要加入一個web組態檔然後打開新加入的web.config把下方的code貼上去

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings/>
        <connectionStrings/>
        <system.web>
            <httpHandlers>
                <add verb="*" path="*.*" type="verifyFile"/>
            </httpHandlers>
            <authorization>
                <deny users="?"/>
            </authorization>
        </system.web>
    </configuration> 

    再來我們要去對IIS作設定,一樣在IIS上對files資料夾按右鍵選擇內容



    依照圖示按下建立



    先在應用程式副檔名的區塊中找到aspx後點一下選擇編輯



    然後把下圖綠框區域的文字全部的複製下來



    按下確定後回到下圖,這次我們要選擇新增



    在上方空白處將您剛剛複製的文字貼上去



    然後將下方的副檔名填上txt並且把步驟二的勾取消掉,如下圖

    ♥小提醒:本範例僅示範txt這個副檔名,如果要加其他的方法一樣,就不多作說明了


    按下確定回到下方圖片的樣子,按下移除

    這樣子就大功告成啦,您可以馬上試試看隨便丟一個txt的檔案到files資料夾中,然後直接輸入網址系統就會將您導入登入頁面要求你登入啦。

    如果你想知道原理與運作方式可以由下方參考網址的網址觀看



    http://www.dotblogs.com.tw/topcat/archive/2008/03/06/1255.aspx