2017-01-24 12 views
0

我一直工作在一個腳本給用戶發送到登陸頁面,提供使用的HttpCookie他們尚未看到今天:布爾的HttpCookie重定向到着陸頁不能正常工作成功

bool userVisited = false; 
HttpCookie cookie = Request.Cookies["Hoarding"]; 
if (cookie == null) 
{ 
    cookie = new HttpCookie("Hoarding"); 
    cookie.Values.Add("userVisitedSplash", "true"); 
    cookie.Expires = DateTime.Now.AddDays(1); 
    cookie.HttpOnly = true; 
    this.Page.Response.AppendCookie(cookie); 
} 
else 
{ 
    if (!Boolean.TryParse(cookie.Values["userVisitedSplash"], out userVisited)) 
    { 
     userVisited = false; 
    } 
    else 
    { 
     Response.Redirect("/default-splash.aspx"); 
    } 
} 

此代碼成功重定向,但一旦在着陸頁上按下輸入網站,它就會重新導向到飛濺。沒有考慮到它已經訪問過它。

這裏有什麼明顯的錯誤嗎?

+0

您是否看到在瀏覽器中設置的cookie? – VisualBean

+0

問題是如果有cookie並且TryParse成功,最後的'else'分支將被採用。 – user1429080

回答

2

可以請你試試這個;

bool userVisited = false; 
     HttpCookie cookie = Request.Cookies["Hoarding"]; 
     if (cookie == null) 
     { 
      cookie = new HttpCookie("Hoarding"); 
      cookie.Values.Add("userVisitedSplash", "true"); 
      cookie.Expires = DateTime.Now.AddDays(1); 
      cookie.HttpOnly = true; 
      this.Page.Response.AppendCookie(cookie); 
      Response.Redirect("/default-splash.aspx"); 
     } 
     else 
     { 
      if (Boolean.TryParse(cookie.Values["userVisitedSplash"], out userVisited)) 
      { 
       if (!userVisited) 
       { 
        Response.Redirect("/default-splash.aspx"); 
       } 

      } 
      else 
      { 
       Response.Redirect("/default-splash.aspx"); 
      } 
     } 
+0

然而它並不完美。 – esertbas

+0

你的天才!不夠感謝你!祝你好日子:-) – davvv