首页 新闻 会员 周边 捐助

cookie.expries无效

0
悬赏园豆:5 [已关闭问题] 关闭于 2013-05-28 07:58

/// <summary>
/// 将user信息写入cookie
/// </summary>
/// <param name="info">user信息类</param>
public static void SetUser(user info)
{
//创建一个cookie对象实例 , 表式为会话cookie
HttpCookie cookie = new HttpCookie("USERINFO");
//设置值
SetExpires(cookie, cookieMinute);
string userInfo = info.id + "," + info.name + "," + info.account + ","+info.Class+"," + info.grade + "," +info.enable+","+info.admin+","+info.groupnum+","+info.isLeader+","+info.lo_ip;
//将数据加密
InfoSet infoset = new InfoSet();
string strEncrypto =infoset.EncryptoLowest(userInfo);
//将加密后的数据设置给cookie
cookie.Value = strEncrypto;
cookie.Expires = DateTime.Now.AddHours(11);
//将cookie发送发服务器
HttpContext.Current.Response.AppendCookie(cookie);
//HttpContext.Current.Response.Write("<script>alert('CookieUser过期时间:"+cookie.Expires.ToString()+"!');</script>");
}

 

/// <summary>
/// 获得user信息cookie值
/// </summary>
/// <returns>user信息类</returns>
public static user GetUserInfo()
{
//查看用户是否存在
if (HttpContext.Current.Request.Cookies["USERINFO"] == null)
{
HttpContext.Current.Response.Write("<script>alert('CookieUser提示:您还没有登陆或者登录超时,请重新登录!');window.location.href='" + HttpContext.Current.Request.ApplicationPath + "/login.aspx';</script>");
return null;
}
else
{
try
{
//得到用户数据
HttpCookie cook = HttpContext.Current.Request.Cookies["USERINFO"];
//HttpContext.Current.Response.Write("<script>alert('CookieUser过期时间:" + cook.Expires.ToString() + "!');</script>");
//取值
string userInfo = cook.Value;
//将数据解密
InfoSet infoset = new InfoSet();
string strDecrypto = infoset.DecryptoLowest(userInfo);
//截取
string[] strInfo = strDecrypto.Split(',');
//创建GlobalUserInfo对象并赋值
if (HttpContext.Current.Session["uid"].ToString() == strInfo[0])
{
user userinfo = new user(int.Parse(strInfo[0]), strInfo[1], strInfo[2], strInfo[3], int.Parse(strInfo[4]),bool.Parse(strInfo[5]),bool.Parse(strInfo[6]),strInfo[7],bool.Parse(strInfo[8]),strInfo[9]);
//返回GlobalUserInfo对象
return userinfo;
}
else
return null;
}
catch
{
HttpContext.Current.Response.Write("<script>alert('CookieUser提示:您还没有登陆或者登录超时,请重新登录!');window.location.href='" + HttpContext.Current.Request.ApplicationPath + "/login.aspx';</script>");
return null;
}

}
}

 

到这里就不成功,获得的过期时间是0001-00-00.请问是什么回事啊?

过期的风子的主页 过期的风子 | 初学一级 | 园豆:148
提问于:2013-01-21 10:45
< >
分享
所有回答(1)
0

/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
/// <param name="strValue">过期时间(分钟)</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);

}

试试这个方法。

telang | 园豆:646 (小虾三级) | 2013-01-21 11:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册