Friday, August 19, 2016

Cookies

  • is a client side storage of your variables. 
  • It stored on client machine by browser physically. It's scope is machine wide.
  • Use for authentication, shopping cart content
  • Cookies cannot carry virus or install malware, but can be spyware to track user's browsing activities.
  • set expires to .addDays(-1) to delete cookies
Limitation of Cookies
  • Size of cookies is limited to 4096 bytes.
  • Total 20 cookies can be used on a single website; if you exceed this browser will delete older cookies.
  • End user can stop accepting cookies by browsers, so it is recommended to check the users’ state and prompt the user to enable cookies.
  • If user clear cookie , we cannot get back
    • Persistent Cookies
      • Remain in client computer, even after browser is closed, you can configure how long the cookies expires.
    • Non-Persistent Cookies
      • is set when you don't set expires property, it will remain in memory until the browser is closed.
      • Good for public computer
Because of this :

1.    You should not store sensitive data on cookie.
2.    Cookie has no effect on server resources.
3.    Cookie expires at specified date by you.

Request.Browser.Cookies to check the browser support cookies.
Check Browser Cookies enable or disabled ? write own code to check if... else with Querystring



Example 1:
HttpCookie userInfo = new HttpCookie("userInfo");
userInfo["UserName"] = "Annathurai";
userInfo["UserColor"] = "Black";
userInfo.Expires.Add(new TimeSpan(0, 1, 0));       
Response.Cookies.Add(userInfo);

HttpCookie reqCookies = Request.Cookies["userInfo"];
if (reqCookies != null)
{
User_name = reqCookies["UserName"].ToString();
 User_color = reqCookies["UserColor"].ToString();        
}

Example 2
Response.Cookies["userName"].Value = "Annathurai";                     
Response.Cookies["userColor"].Value = "Black";

User_Name = Request.Cookies["userName"].Value;        
User_Color = Request.Cookies["userColor"].Value;

No comments:

Post a Comment