如何使用Cookie

raymond418 2009-03-25 10:09:14
我刚学asp.net,请问如何使用Cookie实现某一个用户登录后显示上次访问时间以及访问次数?麻烦写下关键代码,谢谢
...全文
132 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongcha99 2009-03-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Zhanlixin 的回复:]
protected void Button1_Click(object sender, EventArgs e)
{
Response.Cookies["LastTime"].Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
Response.Cookies["LastTime"].Expires = DateTime.Now.AddDays(60);
int intTotTimes = 0;
if (Request.Cookies["TotTimes"] != null)
{
intTotTimes = Convert.ToInt16(Request.Cookies["TotTimes…
[/Quote]

不错,给我点分
  • 打赏
  • 举报
回复
学习~
wsqjg 2009-03-25
  • 打赏
  • 举报
回复
给你发一个通用的方法。
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(cookie);

}

/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
/// <param name="expires">过期时间(分钟)</param>
public static void WriteCookie(string strName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
cookie.Expires = DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);

}

/// <summary>
/// 读cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <returns>cookie值</returns>
public static string GetCookie(string strName)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
{
return HttpContext.Current.Request.Cookies[strName].Value.ToString();
}

return "";
}
tsp860901 2009-03-25
  • 打赏
  • 举报
回复
。。。。。
goodluckalong 2009-03-25
  • 打赏
  • 举报
回复
2楼正解
diershi 2009-03-25
  • 打赏
  • 举报
回复
偶也是初学者,来学习下!!
liudanking 2009-03-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Zhanlixin 的回复:]
protected void Button1_Click(object sender, EventArgs e)
{
Response.Cookies["LastTime"].Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
Response.Cookies["LastTime"].Expires = DateTime.Now.AddDays(60);
int intTotTimes = 0;
if (Request.Cookies["TotTimes"] != null)
{
intTotTimes = Convert.ToInt16(Request.Cookies["TotTimes…
[/Quote]
UP
就是这个了。
Zhanlixin 2009-03-25
  • 打赏
  • 举报
回复
protected void Button1_Click(object sender, EventArgs e)
{
Response.Cookies["LastTime"].Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
Response.Cookies["LastTime"].Expires = DateTime.Now.AddDays(60);
int intTotTimes = 0;
if (Request.Cookies["TotTimes"] != null)
{
intTotTimes = Convert.ToInt16(Request.Cookies["TotTimes"].Value); ;
}
intTotTimes = intTotTimes + 1;
Response.Cookies["TotTimes"].Value = intTotTimes.ToString();
Response.Cookies["TotTimes"].Expires = DateTime.Now.AddDays(60);

}


protected void Button2_Click(object sender, EventArgs e)
{
if (Request.Cookies["LastTime"] != null)
{
this.TextBox1.Text = Request.Cookies["LastTime"].Value;
}
if (Request.Cookies["TotTimes"] != null)
{
this.TextBox2.Text = Request.Cookies["TotTimes"].Value;
}

}
hongcha99 2009-03-25
  • 打赏
  • 举报
回复
关注中

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧