Teknik coding untuk perlindungan terhadap SQL Injection (Part 5)

Diskusi Umum Teknik coding untuk perlindungan terhadap SQL Injection (Part 5)

  • 20 April 2012 8:22
    Moderator
     
     

    ASP.NET 1.1 C#

    Anda dapat menyaring semua query-string, form dan nilai cookie yang masuk dengan menjalankan kode selama BeginRequest event. Sebuah lokasi pusat untuk mendaftarkan kode ini terdapat dalam website file global.asax. Contoh kode di bawah ini akan memeriksa data yang masuk dan secara otomatis mengarahkan ulang ke halaman yang disebut "Error.aspx" jika urutan karakter yang mencurigakan ditemukan.

    Pertama, Anda akan perlu menambahkan namespace import baru di atas file global.asax anda:

    <%@ Import namespace="System.Globalization" %>

    Lalu tempatkan definisi variabel berikut dan private function di suatu tempat dalam file global.asax anda di tag <script>:

        //Defines the set of characters that will be checked. 

        //You can add to this list, or remove items from this list, as appropriate for your site 

        public static string[] blackList = {"--",";--",";","/*","*/","@@","@", 

                                             "char","nchar","varchar","nvarchar", 

                                             "alter","begin","cast","create","cursor","declare","delete","drop","end","exec","execute", 

                                             "fetch","insert","kill","open", 

                                             "select", "sys","sysobjects","syscolumns", 

                                             "table","update"}; 

     

        //The utility method that performs the blacklist comparisons 

        //You can change the error handling, and error redirect location to whatever makes sense for your site. 

        private void CheckInput(string parameter) 

        { 

            CompareInfo comparer = CultureInfo.InvariantCulture.CompareInfo; 

     

            for (int i = 0; i < blackList.Length; i++) 

            { 

                if (comparer.IndexOf(parameter,blackList[i],CompareOptions.IgnoreCase) >= 0) 

                { 

                    // 

                    //Handle the discovery of suspicious Sql characters here 

                    // 

                    Response.Redirect("~/Error.aspx");  //generic error page on your site 

                } 

            } 

        }

    Langkah terakhir tempatkan definisi fungsi berikut dalam tag <script> file global.asax anda. Definisi fungsi ini akan memberitahu ASP.NET untuk menjalankan pemeriksaan string selama BeginRequest event. Jika file global.asax anda sudah memiliki fungsi yang disebut Application_BeginRequest, anda harus menempatkan isi dari definisi fungsi di bawah ini ke dalam versi yang ada dari Application_BeginRequest.

        void Application_BeginRequest(object sender, EventArgs e)  

        { 

            foreach (string key in Request.QueryString) 

                CheckInput(Request.QueryString[key]); 

            foreach (string key in Request.Form) 

                CheckInput(Request.Form[key]); 

            foreach (string key in Request.Cookies) 

                CheckInput(Request.Cookies[key].Value);                 

        }


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.