我在登录操作中进行了下面的处理
HttpCookie cookie_year = new HttpCookie(CookiePrefix+"other_cook",string.Empty);
cookie_year.Values.Add("LastLoginTime" ,LastLoginTime);
cookie_year.Expires = DateTime.Now.AddDays(365);
cookie_year.Domain = HttpContext.Current.Request.Url.Host;
cookie_year.Path = "/";
HttpContext.Current.Response.AppendCookie(cookie_year);
我在注销操作中进行了下面的处理
HttpCookie cookie_year = new HttpCookie(CookiePrefix+"other_cook",string.Empty);
cookie_year.Values.Add("LastLogoutTime" ,LastLogoutTime);
cookie_year.Expires = DateTime.Now.AddDays(365);
cookie_year.Domain = HttpContext.Current.Request.Url.Host;
cookie_year.Path = "/";
HttpContext.Current.Response.AppendCookie(cookie_year);我发现我登陆之后Cookie文件中就只有LastLoginTime,而注销之后就只有LastLogoutTime
我希望我的代码只是在登录时改写LastLoginTime,注销时改写LastLogoutTime
而不是登录时把LastLogoutTime给弄没了,注销时把LastLoginTime给弄没了
我的代码哪里有问题吗?

解决方案 »

  1.   

    HttpContext.Current.Response.AppendCookie
    对于cookie每次 的 操作都是 重写了的你把logintime先读取出来 注销时候一起存进去就是...楼主应该不用把代码写出来了吧:)
      

  2.   

    就是说我要写LastLoginTime就写LastLoginTime,而不会破坏原有的,没有这样线程的方法吗?
      

  3.   

    写得这么复杂Reponse.Cookies["xxx"].value="xxxx"Reponse.Cookies["xxx"].express = datetime.now.addday(10) ;大小写请自己改一下
    要注销的话直接把它的过期时间改到今天之前.就自动注销了.. 用Remove 方法是错的
      

  4.   

    我没有用remove我的代码如下
    欢迎指正/// <summary>
    /// 注销某个用户
    /// </summary>
    public static void SignOut()
    {
    string CookiePrefix = HttpRuntime.Cache["CookiePrefix"].ToString();
    HttpCookie cookie = new HttpCookie(CookiePrefix+"user_cook",string.Empty);
    cookie.Values.Add("userRole" ,"1");
    cookie.Values.Add("userID" ,"0");
    cookie.Values.Add("userNickName" ,"");
    //string LastLogoutTime = System.DateTime.Now.Date.ToString();//记录最后离开日期
    string LastLogoutTime = System.DateTime.Now.ToString();//记录最后离开时间
    cookie.Expires = DateTime.Now.AddDays(365);
    string CookieDomain;
    if(HttpRuntime.Cache["CookieDomain"]!=null)
    {
    CookieDomain = HttpRuntime.Cache["CookieDomain"].ToString();
    }
    else
    {
    CookieDomain = HttpContext.Current.Request.Url.Host;
    } //cookie.Domain = CookieDomain;//只能是单一域名才行
    cookie.Domain = HttpContext.Current.Request.Url.Host;
    cookie.Path = "/";
    HttpContext.Current.Response.AppendCookie(cookie);

    HttpCookie cookie_year = new HttpCookie(CookiePrefix+"other_cook",string.Empty);
    cookie_year.Values.Add("LastLogoutTime" ,LastLogoutTime);
    cookie_year.Expires = DateTime.Now.AddDays(365);
    cookie_year.Domain = HttpContext.Current.Request.Url.Host;
    cookie_year.Path = "/";
    HttpContext.Current.Response.AppendCookie(cookie_year);
    }
    /// <summary>
    /// 设置登陆成功用户的Cookie
    /// </summary>
    /// <param name="userRole"></param>
    /// <param name="userID"></param>
    /// <param name="userNickName"></param>
    /// <param name="day"></param>
    public static void setCookie(int userRole,string userID,string userName,string userNickName,int day)
    {
    string CookiePrefix = HttpRuntime.Cache["CookiePrefix"].ToString();
    HttpCookie cookie = new HttpCookie(CookiePrefix+"user_cook",string.Empty);
    cookie.Values.Add("userRole" ,userRole.ToString());
    cookie.Values.Add("userID" ,userID);
    cookie.Values.Add("userName" ,userName);
    cookie.Values.Add("userNickName" ,userNickName);
    //string LastLoginTime = System.DateTime.Now.Date.ToString();//记录最后登陆日期
    string LastLoginTime = System.DateTime.Now.ToString();//记录最后登陆时间
    cookie.Expires = DateTime.Now.AddDays(day);

    string CookieDomain;
    if(HttpRuntime.Cache["CookieDomain"]!=null)
    {
    CookieDomain = HttpRuntime.Cache["CookieDomain"].ToString();
    }
    else
    {
    CookieDomain = HttpContext.Current.Request.Url.Host;
    } //cookie.Domain = CookieDomain;//不能有多个域名
    cookie.Domain = HttpContext.Current.Request.Url.Host;
    cookie.Path = "/";
    HttpContext.Current.Response.AppendCookie(cookie);

    HttpCookie cookie_year = new HttpCookie(CookiePrefix+"other_cook",string.Empty);
    cookie_year.Values.Add("LastLoginTime" ,LastLoginTime);
    cookie_year.Expires = DateTime.Now.AddDays(365);
    cookie_year.Domain = HttpContext.Current.Request.Url.Host;
    cookie_year.Path = "/";
    HttpContext.Current.Response.AppendCookie(cookie_year);
    }
      

  5.   

    最后生成的Cookie形如netweb_other_cook
    &LastLogoutTime=2007-4-12 10:42:27(应该还有个LastLoginTime)
    www.eepw.com.cn/
    1536
    2916723584
    29924221
    725272384
    29850796
    *
    netweb_guest_cook
    &GuestID=11417b6b-1e19-43d7-b908-34d6274cadc5&GuestNickName=游客
    www.eepw.com.cn/
    1536
    1445726208
    29920827
    3651742304
    29847401
    *
    netweb_user_cook
    &userRole=1&userID=0&userNickName=
    www.eepw.com.cn/
    1536
    2916723584
    29924221
    724962384
    29850796
    *
      

  6.   

    我把
    HttpCookie cookie_year = new HttpCookie(CookiePrefix+"other_cook",string.Empty);
    改成了
    HttpCookie cookie_year = HttpContext.Current.Response.Cookies[CookiePrefix+"other_cook"];以为可以了,但是还是不行。我理解的有问题吗?