2013-04-20 78 views
0

創建了這個簡單的類來創建cookie。但一次又一次地創建cookie。CookieHelper錯誤,一次又一次地創建cookie

using System; 
using System.IO; 
using System.Web; 
using System.Web.UI; 

/// <summary> 
/// Summary description for CookieHelper 
/// </summary> 
public class CookieHelper 
{ 
    private Page _page = null; 
    public bool success { get; private set; } 

    public bool CreateSuccess { get; private set; } 
    protected bool SaveSuccess { get; private set; } 
    protected bool DeleteSuccess { get; private set; } 

    public bool IsExisting { get;private set; } 
    public string cookieName { get; set; } 

    public CookieHelper(Page page) 
    { 
     _page = page; 
     CreateSuccess = SaveSuccess = IsExisting = DeleteSuccess = false; 
    } 

    public bool Exist(string cookieName) 
    { 
     if (string.IsNullOrEmpty(cookieName)) 
      throw new ArgumentNullException("cookieName can't be null"); 
     else 
     { 
      try 
      { 
       if (_page.Request.Cookies[cookieName] == null) 
       { 
        IsExisting = false; 
        return false; 
       } 
       else 
       { 
        IsExisting = true; 
        return true; 
       } 
      } 
      catch (ArgumentException) 
      { 
       throw new ArgumentException("cookieName given to Exist check function is invalid or not string"); 
      } 
     } 
    } 

    public void CreateCookie(string cookieName, int expiryTimeInMinutes=1, bool secure = true) 
    { 
     if (string.IsNullOrEmpty(cookieName)) 
      throw new ArgumentNullException("cookieName can't be null"); 
     else 
     { 
      try 
      { 
       if (Exist(cookieName)) 
       { 
        IsExisting = true; 
        return; 
       } 
       else 
       { 
        HttpCookie cookie=new HttpCookie(cookieName); 
        cookie.Expires = DateTime.Now.AddMinutes(expiryTimeInMinutes); 
        cookie.Secure = secure; 
        CreateSuccess = true; 
        IsExisting = true; 
       } 
      } 
      catch (ArgumentException) 
      { 
       throw new ArgumentException(string.Format("CookieName type mismatch. The parameter is of type '{0}', was expecting '{1}'.", 
        cookieName.GetType(), "a".GetType())); 
      } 
     } 
    } 

    public bool SaveInCookie(string cookieName, string valueKey, string valueToBeStored, int expiryTimeInMinutes, bool secure = true) 
    { 
     SaveSuccess = false; 
     if (typeof(string) != cookieName.GetType() || typeof(string) != valueKey.GetType() || typeof(string) != valueToBeStored.GetType() || typeof(bool) != secure.GetType() || typeof(int) != expiryTimeInMinutes.GetType()) 
      throw new ArgumentException("Parameter type mismatch"); 
     else 
     { 
      try 
      { 
       HttpCookie cookie = null; 
       if (_page.Request.Cookies[cookieName] == null) 
       { 
        cookie = new HttpCookie(cookieName); 
        CreateSuccess = true; 
        IsExisting = true; 
       } 
       else 
       { 
        cookie = _page.Request.Cookies[cookieName]; 
        IsExisting = true; 
        CreateSuccess = false; 
       } 

       cookie.Values.Add(valueKey, valueToBeStored); 
       cookie.Expires = DateTime.Now.AddMinutes(expiryTimeInMinutes); 
       cookie.Secure = secure; 
       _page.Response.Cookies.Add(cookie); 
      } 
      catch (Exception) 
      { 
       SaveSuccess = false; 
       throw new Exception("Cookie Save Failed"); 
      } 
      return SaveSuccess; 
     } 
    } 

    public string GetCookieValue(string cookieName, string valueKey = null) 
    { 
     if (typeof(string) != cookieName.GetType() || typeof(string) != valueKey.GetType()) 
      throw new ArgumentException("Parameter type mismatch"); 
     else 
     { 
      string keyExist = string.Empty; 
      try 
      { 
       if (Exist(cookieName) == IsExisting) 
       { 
        keyExist = (string)_page.Request.Cookies[cookieName][valueKey]; 
        return ((string.IsNullOrEmpty(keyExist)) ? String.Empty : _page.Request.Cookies[cookieName].Values[valueKey] 
         ); 
       } 
       else 
        throw new FileNotFoundException("Cookie with the name provided doesnot exist"); 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 
    } 

    public bool DeleteCookie(string cookieName) 
    { 
     if (typeof(string) != cookieName.GetType()) 
      throw new ArgumentException("Parameter type mismatch"); 
     else 
     { 
      try 
      { 
       if (_page.Request.Cookies[cookieName] != null) 
       { 
        HttpCookie myCookie = new HttpCookie(cookieName); 
        myCookie.Expires = DateTime.Now.AddDays(-1d); 
        _page.Response.Cookies.Add(myCookie); 
        IsExisting = false; 
       } 
       return true; 
      } 
      catch (Exception ex) 
      { throw ex; } 
     } 
    } 
} 

調用這個類在我的網頁原樣

protected void Page_Load(object sender, EventArgs e) 
    { 
     CookieHelper cook = new CookieHelper(this.Page); 
     if (!cook.Exist("Preferences")) 
     { 
      cook.CreateCookie("Preferences",2); 
      Response.Write("Created"); 
     } 
     else 
      Response.Write("Not"); 

    } 

給我總是創建。我添加了斷點並檢查了調試,發現編譯器總是進入創建部分。請建議我創建這個幫助類。

回答

0

這樣看來,你在你的CreateCookie else條件缺

Response.Cookies.Add(myCookie);

因此,它看起來像:

HttpCookie cookie=new HttpCookie(cookieName); 
cookie.Expires = DateTime.Now.AddMinutes(expiryTimeInMinutes); 
cookie.Secure = secure; 
CreateSuccess = true; 
IsExisting = true; 
Response.Cookies.Add(myCookie); 

這可以確保cookie將被添加到響應,因此瀏覽器知道每次訪問時間重播吧。

這是一個good starting place有關cookie表單環境中cookie的信息。

+0

與_page.Response.Cookies.Add(cookie)類似嗎?因爲在.cs中這樣做給了我相同的結果 – 2013-04-20 21:43:26

+0

假設'_page'的類型是'System.Web.UI.Page',那麼是的 – 2013-04-21 08:35:52

+0

我按照建議完成了,每次仍然創建cookie。 – 2013-04-21 12:24:54

相關問題