Cookie的使用和设置有效期

    protected void Page_Load(object sender, EventArgs e)
    {
        //设置Cookies
        HttpCookie cookie = new HttpCookie("UserName","ZhangSan");
        this.Request.Cookies.Add(cookie);

        //设置Cookies有效期
        DateTime time = DateTime.Now;
        TimeSpan span = new TimeSpan(0, 0, 15, 0, 0);//时间间隔
        cookie.Expires = time.Add(span);//设置有效期为15分钟

        //读取Cookies
        string userName = this.Request.Cookies[0].Value;
        this.Response.Write(userName);

        //写入Cookies
        this.Response.SetCookie(cookie);
    }

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页