C#でSQLインジェクションを防ぐにはどうすればよいですか?
Web サイトのセキュリティは、すべての Web サイト開発者および運営者にとって最も懸念される問題です。ひとたびWebサイトに脆弱性が存在すると、多大な損失が生じることは避けられません。 Web サイトのセキュリティを向上させるには、まず Web サイトをインジェクションから保護する必要があります。
C# で SQL インジェクションを防ぐいくつかの方法を紹介します。
方法 1:
Web.config ファイルの下に次のタグを追加します:
< appSettings> < add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" /> < /appSettings>
キーは < saveParameters> で、その後の値は "OrderId-int32" などです。ここで、"-" は先頭はパラメータを示し、名前は OrderId などで、後ろの int32 はデータ型を示します。
方法 2:
Global.asax に次の段落を追加します:
protected void Application_BeginRequest(Object sender, EventArgs e){ String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings["safeParameters"].ToString()。Split(','); for(int i= 0 ;i < safeParameters.Length; i++){ String parameterName = safeParameters[i].Split('-')[0]; String parameterType = safeParameters[i].Split('-')[1]; isValidParameter(parameterName, parameterType); } } public void isValidParameter(string parameterName, string parameterType){ string parameterValue = Request.QueryString[parameterName]; if(parameterValue == null) return; if(parameterType.Equals("int32")){ if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.aspx"); } else if (parameterType.Equals("USzip")){ if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.aspx"); } else if (parameterType.Equals("email")){ if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.aspx"); } }
方法 3:
文字列フィルタリング クラスを使用する
/**//// < summary> /// 处理用户提交的请求 /// < /summary> public static void StartProcessRequest() { // System.Web.HttpContext.Current.Response.Write("< script>alert('dddd');< /script>"); try { string getkeys = ""; //string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString(); if (System.Web.HttpContext.Current.Request.QueryString != null) { for(int i=0;i< System.Web.HttpContext.Current.Request.QueryString.Count;i++) { getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i]; if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys],0)) { //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true"); System.Web.HttpContext.Current.Response.Write("< script>alert('请勿非法提交!');history.back();< /script>"); System.Web.HttpContext.Current.Response.End(); } } } if (System.Web.HttpContext.Current.Request.Form != null) { for(int i=0;i< System.Web.HttpContext.Current.Request.Form.Count;i++) { getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i]; if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys],1)) { //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true"); System.Web.HttpContext.Current.Response.Write("< script>alert('请勿非法提交!');history.back();< /script>"); System.Web.HttpContext.Current.Response.End(); } } } } catch { // 错误处理: 处理用户提交信息! } } /**//// < summary> /// 分析用户请求是否正常 /// < /summary> /// < param name="Str">传入用户提交数据< /param> /// < returns>返回是否含有SQL注入式攻击代码< /returns> private static bool ProcessSqlStr(string Str,int type) { string SqlStr; if(type == 1) SqlStr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare "; else SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare"; bool ReturnValue = true; try { if (Str != "") { string[] anySqlStr = SqlStr.Split('|'); foreach (string ss in anySqlStr) { if (Str.IndexOf(ss)>=0) { ReturnValue = false; } } } } catch { ReturnValue = false; } return ReturnValue; } #endregion } }
推奨される関連ビデオ チュートリアル: "C#Tutorial"
以上がC#でSQLインジェクションを防ぐにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









C# を使用した Active Directory のガイド。ここでは、Active Directory の概要と、C# での動作方法について、構文と例とともに説明します。

C# データ グリッド ビューのガイド。ここでは、SQL データベースまたは Excel ファイルからデータ グリッド ビューをロードおよびエクスポートする方法の例について説明します。

マルチスレッドと非同期の違いは、マルチスレッドが複数のスレッドを同時に実行し、現在のスレッドをブロックせずに非同期に操作を実行することです。マルチスレッドは計算集約型タスクに使用されますが、非同期はユーザーインタラクションに使用されます。マルチスレッドの利点は、コンピューティングのパフォーマンスを改善することですが、非同期の利点はUIスレッドをブロックしないことです。マルチスレッドまたは非同期を選択することは、タスクの性質に依存します。計算集約型タスクマルチスレッド、外部リソースと相互作用し、UIの応答性を非同期に使用する必要があるタスクを使用します。
