asp.net 使用cookie保存用户信息和读取

2025-01-04 04:13:29
推荐回答(3个)
回答1:

保存Cookies代码:
protected void btnSetCookiesClick(object sender, EventArgs e)
{
cookiesManagerTest1.AddSubkey(txtSetCookiesName.Text,txtSetCookiesValue.Text);
Response.Redirect("CookiesManagerGet.aspx");
}

读取Cookies代码:
protected void btnGetCookiesClick(object sender, EventArgs e)
{
HttpCookie cookies = Request.Cookies["ManagerTest"];
if (cookies.HasKeys)
{
if (!String.IsNullOrEmpty(cookies.Values[txtCookiesName.Text]))
{
txtGetCookies.Text = cookies.Values[txtCookiesName.Text];
}
}
}

其中cookiesManagerTest1是一个自定义用户控件的ID,
控件代码如下:
public class CookiesManager : WebControl
{
#region Private
private string _cookieName = "";
private string _cookieSubkey = "";
private string _cookieValue = "";
private bool _dataBound;
private DateTime _expireDate = DateTime.Now.AddDays(3650);
private HttpCookie _cookie;
#endregion
#region Properties
public string CookieValue
{
get { return GetCookieValue(); }
set { _cookieValue = value; }
}

public string CookieName
{
get { return _cookieName; }
set { _cookieName = value; }
}

public string CookieSubkey
{
get { return _cookieSubkey; }
set { _cookieSubkey = value; }
}

public DateTime ExpireDate
{
get { return _expireDate; }
set { _expireDate = value; }
}
#endregion

#region Contructors
public CookiesManager()
{
}

public CookiesManager(string cookiename, string cookiesubkey, string cookievalue, DateTime expiredate)
{
_cookieName = cookiename;
_cookieSubkey = cookiesubkey;
_cookieValue = cookievalue;
_expireDate = expiredate;
}
#endregion

#region Cookie Control event
///


/// Setup the CookiesManager instance in the pages controlstate control collection
///

///
protected override void OnInit(EventArgs e)
{
base.OnInit(e);

if (Page.Items[typeof(CookiesManager)] == null)
{
Page.Items[typeof(CookiesManager)] = this;
}

Page.RegisterRequiresControlState(this);
}

///
/// In the control load, if the Cookie doesn't exist, then will CreateCookie
///

///
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

if (!_dataBound)
{
DataBind();
}
if (!CookieExisted())
CreateCookie();
else
RenewCookie();
}

public override void DataBind()
{
base.DataBind();
_dataBound = true;
}
#endregion
#region Public Methods
public string GetCookieValue()
{
string tmpValue = "";
if (CookieExisted())
{
if (string.IsNullOrEmpty(CookieSubkey))
tmpValue = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies[CookieName].Value);
else
tmpValue = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies[CookieName][CookieSubkey]);
}
return tmpValue;
}

public string GetCookieValue(string cookieSubkey)
{
string tmpValue = "";
if (CookieExisted())
{
tmpValue = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies[CookieName][cookieSubkey]);
}
return tmpValue;
}

///
/// this function remove a subkey, and will do nothing if the subkey doesn't exist
///

///
public void RemoveSubkey(string subkeyname)
{
if (CookieExisted())
{
_cookie = HttpContext.Current.Request.Cookies[CookieName];
if (_cookie.Values[subkeyname] != null)
_cookie.Values.Remove(subkeyname);
}
}

///
/// this function adds a new subkey to the existing cookie, if the subkey already existed,
/// then the fucntion will update the value of the subkey
///

///
///
public void AddSubkey(string subkeyname, string subkeyvalue)
{
if (CookieExisted())
{
_cookie = HttpContext.Current.Request.Cookies[CookieName];
if (_cookie.Values[subkeyname] == null)
_cookie.Values.Add(subkeyname,subkeyvalue);
else
_cookie.Values[subkeyname] = subkeyvalue;
_cookie.Expires = ExpireDate;
}
}

///
/// this function set the cookie.Expires to the ExpireDate
///

public void RenewCookie()
{
if (CookieExisted())
{
_cookie = HttpContext.Current.Request.Cookies[CookieName];
_cookie.Expires = ExpireDate;
}
}

///
/// this function removes a cookie with the list of cookies with the specific CookieName
///

public void DeleteCookie()
{
if (CookieExisted())
{
_cookie = HttpContext.Current.Request.Cookies[CookieName];
_cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Remove(CookieName);
}
}

#endregion
#region Private Methods
///
/// this function checks whether the Cookie Existed with the specified CookieName
///

///
private bool CookieExisted()
{
return ((HttpContext.Current.Request.Cookies[CookieName] != null) && (!string.IsNullOrEmpty(CookieName)));
}

private void CreateCookie()
{
_cookie = new HttpCookie(CookieName);
HttpContext.Current.Response.Cookies.Add(_cookie);
if (string.IsNullOrEmpty(CookieSubkey))
_cookie.Value = _cookieValue;
else
AddSubkey(CookieSubkey, _cookieValue);
_cookie.Expires = ExpireDate;
if (Core.IsTestMode)
{
HttpContext.Current.Response.Write("CookieName=" + CookieName + "
");
HttpContext.Current.Response.Write("CookieSubkey=" + CookieSubkey + "
");
HttpContext.Current.Response.Write("CookieValue=" + CookieValue + "
");
HttpContext.Current.Response.Write("CookieExisted=" + CookieExisted() + "
");
}
}
#endregion
}

回答2:

写出CookieResponse.Cookies["userName"].Value = "test";
Response.Cookies["userName"].Expires = DateTime.Now.AddMonths(1);读取验证CookieString userName = String.Empty;
if(Request.Cookies["userName"]!=null)
userName = Request.Cookies["userName"].Value;

回答3:

恩,楼上的代码够详细了,你好好看下,很简单的