Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating simple cookies in asp.net c#

Tags:

c#

asp.net

My application needs to store cookies. When a user logs on I want to make sure that if the cookie does not exist create it and store value, but if it does modify it.

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

Thanks for any help

like image 458
user516883 Avatar asked Aug 22 '12 18:08

user516883


1 Answers

You have to use Request.Cookies to get cookie value and Response.Cookies to 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
 }
like image 53
Waqar Janjua Avatar answered Oct 05 '22 05:10

Waqar Janjua