Using cookies to store session in ASP MVC

前端 未结 6 1522
野性不改
野性不改 2021-02-03 10:28

Storing the entire session in a cookie has been standard in Rails for the last few years - is there an easy way to achieve something similar with ASP MVC?

By default, an

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-03 11:01

    depends on what kind of data you want to store in the cookie, if you want just to store string, the following code will do:

    HttpCookie cookie = new HttpCookie("username","sth");
                cookie.HttpOnly = true;
                cookie.Expires = DateTime.Now.AddMonths(3);
                HttpContext.Current.Response.Cookies.Add(cookie);
    

提交回复
热议问题