How to store string in a cookie and retrieve it

前端 未结 2 1375
说谎
说谎 2021-02-01 22:16

I want to store the username in the cookie and retrieve it the next time when the user opens the website. Is it possible to create a cookie which doesnt expires when the browser

2条回答
  •  难免孤独
    2021-02-01 23:00

    In addition to what Shai said, if you later want to update the same cookie use:

    HttpCookie myCookie = Request.Cookies["MyTestCookie"];
    DateTime now = DateTime.Now;
    
    // Set the cookie value.
    myCookie.Value = now.ToString();
    
    // Don't forget to reset the Expires property!
    myCookie.Expires = now.AddYears(50);
    Response.SetCookie(myCookie);
    

提交回复
热议问题