1.What is Cookie?
Cookies is a small piece of information stored on the client machine. This file is located on client machines "C:\Document and Settings\Currently_Login user\Cookie" path. Its is used to store user preference information like Username, Password,City and PhoneNo etc on client machines. We need to import namespace called Systen.Web.HttpCookie before we use cookie.
2.Type of Cookies
Persist Cookie - A cookie has not have expired time Which is called as Persist Cookie
Session Cookies - A cookie has expired time Which is called as Session Cookie
//Code to create a UserName cookie containing the name David.
HttpCookie CookieObject = new HttpCookie("UserName", "David");
Response.Cookies.Add(CookieObject);
//Code to read the Cookie created aboveRequest.Cookies["UserName"].Value;
4. How do you create a Persistent Cookie?
You create a persistent cookie the same way as session cookies except that you set the Expires property to a Date in the future which will store the Cookie to the client computer harddrive.
//Code to create a UserName Persistent Cookie that lives for 10 daysHttpCookie CookieObject = new HttpCookie("UserName", "David");
CookieObject.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(CookieObject);
//Code to read the Cookie created aboveRequest.Cookies["UserName"].Value;
5. What is the difference between Session Cookies and Persistent Cookies?
Persistent Cookies are same as Session Cookies except that, persistent cookies have an expiration date. The expiration date indicates to the browser that it should write the cookie to the client's hard drive. Keep in mind that because a user can delete cookies from their machine that there is no guarantee that a cookie you "drop" on a user machine will be there the next time they visit your site.
6. Cookie's common property
Domain => Which is used to associate cookies to domain.
Secure => We can enable secure cookie to set true(HTTPs).
Value => We can manipulate individual cookie.
Values => We can manipulate cookies with key/value pair.
Expires => Which is used to set expire date for the cookies.
7. Advantages of Cookies
Its clear text so user can able to read it.
We can store user preference information on the client machine.
Its easy way to maintain.
Fast accessing.
1. Cookies do not require any server resources since they are stored on the client.
2. Cookies are easy to implement.
3. You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).
8. Disadvantages of Cookies
If user clear cookie information we can't get it back.
No security.
Each request will have cookie information with page.
1. Users can delete a cookies.
2. Users browser can refuse cookies,so your code has to anticipate that possibility.
3. Cookies exist as plain text on the client machine and they may pose a possible security risk as anyone can open and tamper with cookies.
9. How to clear the cookie information?
we can clear cookie information from client machine on cookie folder
10.To set expires to cookie object
userInfo.Expires = DateTime.Now.AddHours(1);
It will clear the cookie with one hour duration.
11. How do you create a Cookie that never expires?
To create a Cookie that never expires set the Expires property of the Cookie object to DateTime.MaxValue.
12. Are Cookies secure?
No, Cookies are not secure. You must pay attention to the type of data you store in cookies.
1. Cookies are not designed to store critical information so storing passwords in a cookie is a bad idea.
2. Keep the lifetime of a cookie as short as practically possible.
3. Encrypt cookie data to help protect the values stored in the cookie.
13. What is Cookie Dictionary?
A cookie dictionary is a single cookie object that stores multiple pieces of information. You use the Values property to access and assign new values to the cookie dictionary.
14. Give an example using Cookie Dictionary?
//Code to create a Cookie DictionaryHttpCookie CookieObject = new HttpCookie("UserPreference");
//Use the Values property to assign new values to the cookie dictionaryCookieObject.Values.Add("UserName", "David");
CookieObject.Values.Add("Country", "USA");
CookieObject.Values.Add("PreviousVisit", DateTime.Now.ToString());
CookieObject.Expires = DateTime.MaxValue;
//Add the Cookie to the client machine using the Response objectResponse.Cookies.Add(CookieObject);
//Code to read the Cookie created aboveHttpCookie ObjectCookie = Request.Cookies["UserPreference"];
string UserName = ObjectCookie.Values["UserName"];
string Country = ObjectCookie.Values["Country"];
string PreviousVisit = ObjectCookie.Values["PreviousVisit"];
Cookies is a small piece of information stored on the client machine. This file is located on client machines "C:\Document and Settings\Currently_Login user\Cookie" path. Its is used to store user preference information like Username, Password,City and PhoneNo etc on client machines. We need to import namespace called Systen.Web.HttpCookie before we use cookie.
2.Type of Cookies
Persist Cookie - A cookie has not have expired time Which is called as Persist Cookie
Session Cookies - A cookie has expired time Which is called as Session Cookie
3. How can you create Session Cookies?
You can create session cookies by calling the Add method of the Cookies collection on the Response object. The Cookies collection contains individual cookie objects of type HttpCookie.
//Code to create a UserName cookie containing the name David.
HttpCookie CookieObject = new HttpCookie("UserName", "David");
Response.Cookies.Add(CookieObject);
//Code to read the Cookie created aboveRequest.Cookies["UserName"].Value;
4. How do you create a Persistent Cookie?
You create a persistent cookie the same way as session cookies except that you set the Expires property to a Date in the future which will store the Cookie to the client computer harddrive.
//Code to create a UserName Persistent Cookie that lives for 10 daysHttpCookie CookieObject = new HttpCookie("UserName", "David");
CookieObject.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(CookieObject);
//Code to read the Cookie created aboveRequest.Cookies["UserName"].Value;
5. What is the difference between Session Cookies and Persistent Cookies?
Persistent Cookies are same as Session Cookies except that, persistent cookies have an expiration date. The expiration date indicates to the browser that it should write the cookie to the client's hard drive. Keep in mind that because a user can delete cookies from their machine that there is no guarantee that a cookie you "drop" on a user machine will be there the next time they visit your site.
6. Cookie's common property
Domain => Which is used to associate cookies to domain.
Secure => We can enable secure cookie to set true(HTTPs).
Value => We can manipulate individual cookie.
Values => We can manipulate cookies with key/value pair.
Expires => Which is used to set expire date for the cookies.
7. Advantages of Cookies
Its clear text so user can able to read it.
We can store user preference information on the client machine.
Its easy way to maintain.
Fast accessing.
1. Cookies do not require any server resources since they are stored on the client.
2. Cookies are easy to implement.
3. You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).
8. Disadvantages of Cookies
If user clear cookie information we can't get it back.
No security.
Each request will have cookie information with page.
1. Users can delete a cookies.
2. Users browser can refuse cookies,so your code has to anticipate that possibility.
3. Cookies exist as plain text on the client machine and they may pose a possible security risk as anyone can open and tamper with cookies.
9. How to clear the cookie information?
we can clear cookie information from client machine on cookie folder
10.To set expires to cookie object
userInfo.Expires = DateTime.Now.AddHours(1);
It will clear the cookie with one hour duration.
11. How do you create a Cookie that never expires?
To create a Cookie that never expires set the Expires property of the Cookie object to DateTime.MaxValue.
12. Are Cookies secure?
No, Cookies are not secure. You must pay attention to the type of data you store in cookies.
1. Cookies are not designed to store critical information so storing passwords in a cookie is a bad idea.
2. Keep the lifetime of a cookie as short as practically possible.
3. Encrypt cookie data to help protect the values stored in the cookie.
13. What is Cookie Dictionary?
A cookie dictionary is a single cookie object that stores multiple pieces of information. You use the Values property to access and assign new values to the cookie dictionary.
14. Give an example using Cookie Dictionary?
//Code to create a Cookie DictionaryHttpCookie CookieObject = new HttpCookie("UserPreference");
//Use the Values property to assign new values to the cookie dictionaryCookieObject.Values.Add("UserName", "David");
CookieObject.Values.Add("Country", "USA");
CookieObject.Values.Add("PreviousVisit", DateTime.Now.ToString());
CookieObject.Expires = DateTime.MaxValue;
//Add the Cookie to the client machine using the Response objectResponse.Cookies.Add(CookieObject);
//Code to read the Cookie created aboveHttpCookie ObjectCookie = Request.Cookies["UserPreference"];
string UserName = ObjectCookie.Values["UserName"];
string Country = ObjectCookie.Values["Country"];
string PreviousVisit = ObjectCookie.Values["PreviousVisit"];
No comments:
Post a Comment