user516883
user516883

Reputation: 9378

creating simple cookies in asp.net c#

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

Upvotes: 10

Views: 53733

Answers (1)

Waqar Janjua
Waqar Janjua

Reputation: 6123

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
 }

Upvotes: 32

Related Questions