What is Cookies?
Reference from :- http://www.c-sharpcorner.com/uploadfile/annathurai/cookies-in-Asp-Net/
Cookiesis 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.
Type of Cookies?
Cookiesis 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.
Type of Cookies?
- Persist Cookie - A cookie has not have expired time Which is called as Persist Cookie
- Non-Persist Cookie - A cookie has expired time Which is called as Non-Persist Cookie
How
to create a cookie?
Its really easy to create a cookie in the Asp.Net with help of Response object or HttpCookie
Exp 1:
HttpCookie userInfo = new HttpCookie("userInfo");
userInfo["UserName"] = "Annathurai";
userInfo["UserColor"] = "Black";
userInfo.Expires.Add(new TimeSpan(0, 1, 0)); Response.Cookies.Add(userInfo);Exp 2:
Response.Cookies["userName"].Value = "Annathurai"; Response.Cookies["userColor"].Value = "Black";
How to retrieve from cookie?
Its easy way to retrieve cookie value form cookes by help of Request object.
Exp 1:
string User_Name = string.Empty;
string User_Color = string.Empty;
User_Name = Request.Cookies["userName"].Value; User_Color = Request.Cookies["userColor"].Value;Exp 2:
string User_name = string.Empty;
string User_color = string.Empty;
HttpCookie reqCookies = Request.Cookies["userInfo"];
if (reqCookies != null)
{
User_name = reqCookies["UserName"].ToString();
User_color = reqCookies["UserColor"].ToString(); }
When we make request from client to webserver the web server process the request and give the lot of information with big pockets which will have Header information, Metadata, cookies etc., Then repose object can do all the things with browser.
Its really easy to create a cookie in the Asp.Net with help of Response object or HttpCookie
Exp 1:
HttpCookie userInfo = new HttpCookie("userInfo");
userInfo["UserName"] = "Annathurai";
userInfo["UserColor"] = "Black";
userInfo.Expires.Add(new TimeSpan(0, 1, 0)); Response.Cookies.Add(userInfo);Exp 2:
Response.Cookies["userName"].Value = "Annathurai"; Response.Cookies["userColor"].Value = "Black";
How to retrieve from cookie?
Its easy way to retrieve cookie value form cookes by help of Request object.
Exp 1:
string User_Name = string.Empty;
string User_Color = string.Empty;
User_Name = Request.Cookies["userName"].Value; User_Color = Request.Cookies["userColor"].Value;Exp 2:
string User_name = string.Empty;
string User_color = string.Empty;
HttpCookie reqCookies = Request.Cookies["userInfo"];
if (reqCookies != null)
{
User_name = reqCookies["UserName"].ToString();
User_color = reqCookies["UserColor"].ToString(); }
When we make request from client to webserver the web server process the request and give the lot of information with big pockets which will have Header information, Metadata, cookies etc., Then repose object can do all the things with browser.
Comments