2013-07-02 71 views
0

我有下面的代码来检查查询字符串并没有改变:检查查询字符串并没有改变

protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!this.IsPostBack) 
      { 
       Label_Error.Visible = false; 
       string query_string = Request.QueryString["GUID"].ToString(); 
       Session["GUID"] = query_string; 
      } 

      else 
      { 
       string GUID = ""; 

       try 
       { 
        GUID = Session["GUID"].ToString(); 
       } 
       catch (Exception) 
       { 
        Session.Abandon(); 
        Response.Redirect("CheckOutErrorPage.htm"); 
        return; 
       } 

       if (GUID.Equals(Request.QueryString["GUID"].ToString()) == false) 
       { 
        Session.Abandon(); 
        Response.Redirect("CheckOutErrorPage.htm"); 
       } 
      } 
     } 

现在,我有这样的代码在按钮单击事件处理程序,以检查查询字符串的值没有(再次)改为:

protected void ImageButton_LogIn_Click(object sender, ImageClickEventArgs e) 
     { 
      Validation val = new Validation(); 

      string GUID = ""; 
      string query_string = ""; 

      try 
      { 
       GUID = Session["GUID"].ToString(); 
       query_string = Request.QueryString["GUID"].ToString(); 
      } 
      catch (Exception) 
      { 
       Session.Abandon(); 
       Response.Redirect("CheckOutErrorPage.htm"); 
       return; 
      } 

      if (val.EmptyString(GUID) == true || val.checkTransactionGUIDExists(GUID) == false || GUID.Equals(query_string) == false) 
      { 
       Session.Abandon(); 
       Response.Redirect("CheckOutErrorPage.htm"); 
      } 

现在的问题有两个:

1)如果我更改查询字符串中的网址,然后点击按钮,用户不是redi修改为错误页面。 2)如果我更改URL中的查询字符串并在地址栏中输入,则用户不会重定向到错误页面。

我想要的基本上是,当用户重定向到网页时,它将查询字符串保存到会话中。如果用户在地址栏中更改查询字符串的值,并按下地址栏中的回车键或按下我的按钮,他将被重定向到错误页面。

但是,我的代码失败。任何人都可以帮忙吗?谢谢:)

+1

您是否使用调试器来遍历代码并查看哪些条件逻辑已经或未发生? –

+0

谢谢:)我正在调试程序。 – Matthew

回答

2

这是怎么回事?

protected void Page_Load(object sender, EventArgs e) 
{ 
    // Always get the query string no matter how the user go to this page 
    string query_string = Request.QueryString["GUID"].ToString(); 

    // Only store the query string in Session if there is nothing in Session for it 
    if(null == Session["GUID"]) 
    { 
     Session["GUID"] = query_string; 
    } 

    if (!this.IsPostBack) 
    { 
     Label_Error.Visible = false; 
    } 

    // Always check to see if the query string value matches what is in Session 
    string GUID = ""; 
    try 
    { 
     GUID = Session["GUID"].ToString(); 
    } 
    catch (Exception) 
    { 
     Session.Abandon(); 
     Response.Redirect("CheckOutErrorPage.htm"); 
     return; 
    } 

    if (GUID.Equals(Request.QueryString["GUID"].ToString()) == false) 
    { 
     Session.Abandon(); 
     Response.Redirect("CheckOutErrorPage.htm"); 
    } 

这应该解决您Session值的问题,当一个查询字符串放到地址栏,由用户按下回车被覆盖。

+0

是的,我认为是唯一的方法。 –

+0

非常感谢:)我会试试看,并让你知道:) – Matthew

+0

谢谢:)它的工作完美:) – Matthew

1

我觉得你的问题是,Response.Redirect需要false在最后像Response.Redirect("CheckOutErrorPage.htm", false);句子becouse你有它的尝试导管误差内将扔的。

我希望能帮到你。

+0

我会试试看。谢谢:) – Matthew

+2

为了澄清Freak_Droid推荐什么,Response.Redirect()的额外'false'参数停止了'ThreadAbortException'的发生,因为结束了'Response'对象。 –

+0

@karl非常感谢:) –