2012-11-29 31 views
7

非常に簡単な一時的なセキュリティを自分のサイトに追加したいと思います。Global.asaxを使用してセッション変数を設定/確認し、リダイレクトする(ユーザーテスト用)

サイトをテストしているユーザーがハードコードされたパスワードを入力できる「Underconstruction」セッション変数を「false」に設定するページをHome/UnderConstructionで作成しました。

これは私がこれまで持っているものであるが、それはあまりにも多くのリダイレクトになり:

protected void Session_Start(Object sender, EventArgs e) 
    { 
     HttpContext.Current.Session["underconstruction"] = "true"; 
    } 

    protected void Application_AcquireRequestState(Object sender, EventArgs e) 
    { 
       if (HttpContext.Current != null && HttpContext.Current.Session != null) 
       { 
        var underconstruction = HttpContext.Current.Session["underconstruction"]; 
        if (underconstruction != null) 
        { 
         string oc = underconstruction.ToString(); 
         if (oc != "false") Response.Redirect("~/Home/UnderConstruction"); 
        } 
       } 

    } 

は私が行う必要があるだろうかと、この近いですか?

ここでは、動作するようになったコードです:

コントローラコードUnderConstruction Viewの

public ViewResult UnderConstruction() 
    { 
     return View(); 
    } 


    [HttpPost] 
    public ActionResult UnderConstruction(string ocp) 
    { 
     if (ocp == "mypassword") 
     { 
      Session["underconstruction"] = "false"; 
      return RedirectToAction("Index", "Home"); 
     } 
     else 
     { 
      Session["beingredirected"] = "false"; 
      return View(); 
     } 
    } 

Global.asaxの

protected void Session_Start(Object sender, EventArgs e) 
    { 
     HttpContext.Current.Session["underconstruction"] = "true"; 
     HttpContext.Current.Session["beingredirected"] = "false"; 
    } 


    protected void Application_AcquireRequestState(Object sender, EventArgs e) 
    { 
     if (HttpContext.Current != null && HttpContext.Current.Session != null) 
     { 
      bool uc = false; 
      var underconstruction = HttpContext.Current.Session["underconstruction"]; 
      if (underconstruction != null) 
      { 
       uc = Boolean.Parse(underconstruction.ToString()); 
      } 

      bool redirected = false; 
      var beingredirected = HttpContext.Current.Session["beingredirected"]; 
      if (beingredirected != null) 
      { 
       redirected = Boolean.Parse(beingredirected.ToString()); 
      } 

      if (uc && !redirected) 
      { 
       if (Request.HttpMethod == "GET") 
       { 
        HttpContext.Current.Session["beingredirected"] = "true"; 
        Response.Redirect("~/Home/UnderConstruction"); 
       } 
       else if (Request.HttpMethod == "POST") 
       { 
       } 

      } 

      HttpContext.Current.Session["beingredirected"] = "false"; 
     } 
    } 
+0

試しましたか?それは私が始めるところです – Tad

+0

はい、私が言ったように、それは無限ループに終わります。私はそれを修正する方法がわかりません。 – Dave

+0

ちょっと考えましたが、if文を 'if(oc ==" true ")'に変更することはできますか? – Mightymuke

答えて

9

が異なる中~/Home/UnderConstructionですが、ウェブサイト?そうでない場合は、ocが常に真実になるため、常にリダイレクトされませんか?つまり、リクエストしているページのチェックを追加する必要があります。既にUnderConstructionページに行くとリダイレクトをバイパスできますか?

ないページ名をチェックすること素晴らしいアイデアですが、このような何かがうまくいくかもしれないかどうかわから

UPDATE:私はそれをクリーンアップするでしょう

protected void Session_Start(Object sender, EventArgs e) 
{ 
    HttpContext.Current.Session["underconstruction"] = "true"; 
    HttpContext.Current.Session["beingredirected"] = "false"; 
} 

protected void Application_AcquireRequestState(Object sender, EventArgs e) 
{ 
    if (HttpContext.Current != null && HttpContext.Current.Session != null) 
    { 
     bool uc = false; 
     var underconstruction = HttpContext.Current.Session["underconstruction"]; 
     if (underconstruction != null) 
     { 
      uc = Boolean.Parse(underconstruction); 
     } 

     bool redirected = false; 
     var beingredirected = HttpContext.Current.Session["beingredirected"]; 
     if (beingredirected != null) 
     { 
      redirected = Boolean.Parse(beingredirected); 
     } 

     if (uc && !redirected) 
     { 
      HttpContext.Current.Session["beingredirected"] = "true"; 
      Response.Redirect("~/Home/UnderConstruction"); 
     } 

     HttpContext.Current.Session["beingredirected"] = "false"; 
    } 
} 

。なお、例だけにしました一般的な考えを与える。

UPDATE

あなたはコメントで述べたようにロールを使用する場合は、this article from ScottGu's Blogは役立つかもしれません。その少し複雑ですが、上記の解決策として一時的なコードを導入しないという利点があります

+0

新しい答えが助けになるようですが、パスワードを入力しなくても2度目のページに行くと、それは私を可能にします。ほぼそこにあります。私はそれを調べていて、すぐに返信します。 – Dave

+0

@ MightMuke、私がそれを正しく理解していれば、元のエラーが隠れています。 「リダイレクトが多すぎます」という標準誤差は、いくつかの条件を計算してセッションを設定した後で、global.asaxのUnderconstructionページにナビゲートしているためです。次の回り込みでは、グローバルasaxは再び同じサイクルを経ます。出口点はありません。 Session_Startイベントはセッションの作成には使用されません。セッション開始時に発生するロジックが含まれています。 – CodeSpread

+0

@コードスプレッド私は単純に、建設中のページへのナビゲーションを許可するためのチェックが追加されない限り、リダイレクトループを入力することを指摘していました。私のコードは確かにきれいではありません。そのサウンドによって、あなたはもっと良い解決策を持っていると思うでしょう:) – Mightymuke

関連する問題