Creating simple cookies in asp.net c #

My application must store cookies. When the user logs in, I want to make sure that if the cookie does not exist, create it and save the value, but if it changes it.

if(cookieExist)
 {
    cookiename = "value";
 }
else
 {
   create a new cookie 
   then store the value;
 }

Thanks for any help

+5
source share
1 answer

You must use Request.Cookiesto get cookies and Response.Cookiesto add cookies

 string cookievalue ;
 if ( Request.Cookies["cookie"] != null )
 {
    cookievalue = Request.Cookies["cookie"].ToString();
 }
 else
 {
    Response.Cookies["cookie"].Value = "cookie value";
     Response.Cookies["cookie"].Expires = DateTime.Now.AddMinutes(1); // add expiry time
 }
+20
source

All Articles