0

私は既存のプロジェクトを持っているので、ユーザー入力をグローバルにクリーニング/チェックする確実な方法が必要です。

タグを持つマスターページ(親)がformあり、「子」ページにはポストバックコマンド、データベースへの挿入/更新/削除などのフォーム要素が含まれています.

C# のみを使用 - ポストが子ページを通過する前にポストバックをグローバルに傍受できる方法はありますか?

また、ユーザー入力のクリーニングを別の方法で操作するために、テキストボックス/チェックボックス/ラジオボタンなどであるかどうかを確認することは可能ですか?

4

3 に答える 3

1

これは私がそれを機能させるためにしたことです。

  • MasterPageのOnInitイベントをオーバーライドします

  • RequestコレクションのReadOnlyを無効にして、設定可能にします。

  • 投稿された.Netコントロールをループし、値をサニタイズして新しい値を設定します。

  • 読み取り専用を再度有効にします。

グローバルに機能し、肉体労働の日数を節約しました:)

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        if (IsPostBack)
        {
            var fCollection = (NameValueCollection)Request.GetType().GetField("_form", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
            PropertyInfo oReadable = fCollection.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
            oReadable.SetValue(fCollection, false, null);

            foreach (string s in Request.Params.Keys)
            {
                var pCtrl = Page.FindControl(s);

                //Sanitize the posted values (TextHandler.SimpleSanitize is my own static method)
                if (pCtrl != null) fCollection[s] = TextHandler.SimpleSanitize(fCollection[s]);
            }

            oReadable.SetValue(fCollection, true, null);

        }
    }
于 2012-06-26T09:44:21.523 に答える
0

context.BeginRequestイベントを処理するカスタムIHttpModuleを使用して何かを実行できる場合があります。

public class MyHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(OnBeginRequest);
    }

    protected void OnBeginRequest(object sender, EventArgs e)
    {
        var context = ((HttpApplication) sender).Context;
        // do something here, use the context to get request info
    }

    public void Dispose()
    {
    }
}

次のように、HttpModuleをweb.configに登録する必要があります。

<httpModules>
  <add name="MyHttpModule" type="MyProject.MyNamespace.MyHttpModule, MyProject"/>
</httpModules>
于 2012-06-21T13:34:42.210 に答える
-1

MasterPage マークアップに Jquery 関数を追加するだけで、必要なすべてのイベントをインターセプトし、必要な処理を実行できます。必要なコントロールのみをキャッチするには、css クラスを適用できます

于 2012-06-21T12:18:08.300 に答える