Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello All,

I am creating one cookie and setting it's expire to 30 days,but once i close browser,
cookie doesn't persist for second time when user login in a same day itself.

Please find the below code :
C#
public ActionResult SaveUserTypeCookies(string usertype, string returnUrl)
{
   if (Request.Cookies["User"] != null)
   {
       HttpCookie cookie = Request.Cookies["User"];
       cookie.Values["UserType"] = usertype;
       cookie.Expires = DateTime.Now.AddDays(30);
       Response.SetCookie(cookie);
   }
   else
   {
       HttpCookie cookie = new HttpCookie("User");
       //Need to remove below comment at production deployement
       // cookie.HttpOnly = true;
       //cookie.Secure = true;
       cookie.Values["UserType"] = usertype;
       cookie.Expires = DateTime.Now.AddDays(30);
       Response.Cookies.Add(cookie);
   }
   // return RedirectToAction("CreateUpsID");
   return Redirect(returnUrl);
}

Kindly suggest.
Posted
Updated 27-Nov-15 1:20am
v4
Comments
[no name] 27-Nov-15 6:00am    
try Response.AppendCookie(cookie) instead of Response.Cookies.Add(cookie)
Nitin soni 27-Nov-15 6:08am    
Hello Pankaj,
I tried the same ,but it doesn't help. :(
Sinisa Hajnal 27-Nov-15 6:01am    
Does not persist at all or just in the same day? Do you use same browser to login both times? Same computer?
Nitin soni 27-Nov-15 6:06am    
Yes ..I am using same browser and same computer.
Sinisa Hajnal 27-Nov-15 6:37am    
You didn't answer if the cookie doesn't persist at all or just in case when you access the computer again the same day? Your wording indicates that it works properly if you connect later.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900