Guest User

Guest User

  • Tech Writer
  • 515
  • 47k

About Cookies in MVC

Aug 14 2020 10:56 AM
Hello Everyone,
 
I am saving Session values in cookies with following code :
  1. HttpCookie AlvloginCookie = new HttpCookie("AlvLogin");  
  2. //Set the Cookie value.  
  3. AlvloginCookie.Values["Name"] = Session["Name"].ToString();  
  4. AlvloginCookie.Values["UserAuthKey"] = Session["UserAuthKey"].ToString();  
  5. AlvloginCookie.Values["Email"] = Session["Email"].ToString();  
  6. AlvloginCookie.Values["UserId"] = Session["UserId"].ToString();  
  7. AlvloginCookie.Path = Request.ApplicationPath;  
  8. AlvloginCookie.Expires = DateTime.Now.AddDays(365);  
  9. Response.SetCookie(AlvloginCookie);  
  10. Response.Cookies.Add(AlvloginCookie);  
Using that cookies on page using following code :
 
Request.Cookies["UserId"].Value; 
 
The cookies are getting created in browser, but it does not last long for 365 days.
It is getting expired after couple fo hours.
 
Anything I missed or any other suggestion would be really appreciated 
 
Thank you in advance

Answers (6)