问几个cookie问题

x363961461 2011-05-24 10:19:58
对cookie没太多了解,看了些网上例子和介绍就开始做了些事例。 但是不明白的是,开发工具每次停止后。重新运行。 cookie还是空的。 这是怎么回事呢?
我本机没有清空cookie的处理

js和c#的代码都是:开发工具每次停止后。重新运行。 cookie还是空的。

难道是开发工具的临时服务器做了什么处理吗, 还是项目发布后。用户访问时就是正确的。

购物车的存储重新运行。 cookie就丢失。


public static String CookieName = "SHOPPING_INFO";


/// <summary>
/// 添加商品进购物车
/// </summary>
/// <param name="productId"></param>
/// <param name="num"></param>
/// <returns></returns>
public static bool AddProduct(int productId, int num) {
HttpCookie hc = null;
String oldCookieValue;
String NewValue;
hc = HttpContext.Current.Request.Cookies[CookieName];
if (hc == null) //如果Cookie不存在 则直接新建Cookie 保存商品id和数量;
{
hc = new HttpCookie(CookieName);
hc.Expires = DateTime.Now.AddDays(7);
hc.Value = productId.ToString() + "|" + num.ToString();
}
else //假设Cookie存在 则处理
{
oldCookieValue = hc.Value;
NewValue = AddCookieValue(oldCookieValue, productId, num);
hc.Value = NewValue;

}

HttpContext.Current.Response.AppendCookie(hc);

//Cookie写入Response
return true; //结束
}

/// <summary>
/// 获取购物车信息
/// </summary>
/// <returns></returns>
public static string GetShoppingInfo() {
HttpCookie hc = null;
String oldCookieValue;
hc = HttpContext.Current.Request.Cookies[CookieName];
if (hc == null) //如果Cookie存在 则直接新建Cookie 保存商品id和数量;
{
return null;
}
else {
return hc.Value;
}
}



登录票证的存储也是 重新运行开发程序。cookie就丢失


//验证用户名密码
if (new UsersDAL().IsLogin(this.txtName.Value, this.txtPwd.Value)) {


Users userEntity = new UsersDAL().GetEntity(this.txtName.Value);

string status = userEntity.Status.ToString();//获取登录用户的权限


string userId = userEntity.UserID.ToString();//获取用户ID


bool save = IsSave.Checked == true ? true : false;

Authentication.SetCookie(this.txtName.Value, this.txtPwd.Value, status, userId, save);//填入cookie
}

public static void SetCookie(string UserName, string PassWord, string Rights, string usrId, bool save) {

String UserData = UserName + "#" + PassWord + "#" + Rights + "#" + usrId;

//数据放入ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(60), save, UserData);
//数据加密
string enyTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
HttpContext.Current.Response.Cookies.Add(cookie);

}

...全文
78 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
x363961461 2011-05-24
  • 打赏
  • 举报
回复
试了。
在iis中运行。cookie是被永久保存的。 在内置的调试服务器上。cookie总是当此有效的。

我在内置调试服务器上和iis服务器上同时修改了这个cookie。 结果是

iis上还是读的永久的被保存的值。 后者读出来每次还是空值。

就算在调试中更改了 iis中也没反映。 反之一样
x363961461 2011-05-24
  • 打赏
  • 举报
回复
我是用内置的ASP.NET 的web调试器运行的
本机的cookie阻止级别是 中
、、
我试试,在iis中
风骑士之怒 2011-05-24
  • 打赏
  • 举报
回复
AddCookieValue 发来看看
闲游四疯 2011-05-24
  • 打赏
  • 举报
回复
你是不是设置浏览器关闭时自动清空COOKIE文件?
HDNGO 2011-05-24
  • 打赏
  • 举报
回复
你是在IIS里运行的,还是用那个内置的ASP.NET 的web调试器运行的?
如果是后者。。。
嗯,在IIS里加个虚拟目录看看吧~
卷泥巴重来 2011-05-24
  • 打赏
  • 举报
回复
代码没问题,我也同意一楼的看法!你看一下浏览器禁用cookie没有。
x363961461 2011-05-24
  • 打赏
  • 举报
回复
当然没有了。 csdn的登录 我刚才也是直接进来的。
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wxr0323 的回复:]

浏览器禁用cookie没有。
[/Quote]

同问。。
子夜__ 2011-05-24
  • 打赏
  • 举报
回复
浏览器禁用cookie没有。

62,046

社区成员

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

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

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

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