C# CookieHelper错误,一次又一次创建cookie
创建这个简单的类来创建cookie。但是一次又一次地创建cookieC# CookieHelper错误,一次又一次创建cookie,c#,asp.net,webforms,C#,Asp.net,Webforms,创建这个简单的类来创建cookie。但是一次又一次地创建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;
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; }
}
}
}
给我的总是创造的。我添加了断点并检查了调试,发现编译器总是转到创建部分。请建议我创建此帮助器类。您似乎缺少帮助器类
Response.Cookies.Add(mycokie)代码>
在CreateCookie
else条件下
因此,它看起来像:
HttpCookie cookie=new HttpCookie(cookieName);
cookie.Expires = DateTime.Now.AddMinutes(expiryTimeInMinutes);
cookie.Secure = secure;
CreateSuccess = true;
IsExisting = true;
Response.Cookies.Add(myCookie);
这样可以确保将cookie添加到响应中,以便浏览器在每次访问时都能知道该cookie
这是一个关于web表单环境中cookie信息的示例。这类似于_page.Response.cookies.Add(cookie);因为在.cs中这样做会给我相同的结果summing\u page
属于System.Web.UI.page
类型,所以我已经按照建议做了,每次都会创建cookie。
HttpCookie cookie=new HttpCookie(cookieName);
cookie.Expires = DateTime.Now.AddMinutes(expiryTimeInMinutes);
cookie.Secure = secure;
CreateSuccess = true;
IsExisting = true;
Response.Cookies.Add(myCookie);