How to create customized cookies in your program

How to create customized cookies in .net program

I am gonna tell you that how you can create your own cookies in your program.

There is a namespace called System.Web, in which there is a class called HttpCookie, you have to include in your program.

The HttpCookie class gets and sets properties of specific cookies. The HttpCookieCollection class gives methods to store, retrieve, and handle multiple cookies.

System.Web Namespace
— HttpCookie class

Now in the exact location, you have to create a object of that class to create your own cookie.

Syntax:

HttpCookie myCookie = new HttpCookie(“Cookiename”);

You have created a object of HttpCookie Class only and you create your own cookie, now you

can customized your own cookie like you can add key, backcolor, forecolor, font, size, username, lastlogin etc.

You can set the value of your cookie with this code:

mycookie.Values.Add(“key”, “value”);

Now what you think, you can add backcolor and forecolor with this code:

mycookie.Values.Add(“BackColor”, “White”);

mycookie.Values.Add(“BackColor”, RBtnBackColor.SelectedItem.Text);
mycookie.Values.Add(“ForeColor”, RBtnForeColor.SelectedItem.Text);

If you want to set your cookie’s font and size then with the help of this code, you can set

mycookie.Values.Add(“Font”, DdlFont.SelectedItem.Text);
mycookie.Values.Add(“Size”, DdlSize.SelectedItem.Text);

If you want to store something in your cookie, with the help of this code, you can

mycookie.Values.Add(“UserName”, Page.User.Identity.Name); //adding username
mycookie.Values.Add(“LastLogin”, DateTime.Now.ToString()); //adding lastlogin

* How to create customized cookies by HttpCookie

R K Gupta

Hi, This is R K Gupta, an IT professional and Entrepreneur. I have written many articles yet for my websites gsesoftsolutions.com, Hope you like it. Kindly subscribe our feed for any updates. Add me in Facebook and Google+

More Posts - Website

Follow Me:
TwitterFacebookPinterestYouTube

Hi, This is R K Gupta, an IT professional and Entrepreneur. I have written many articles yet for my websites gsesoftsolutions.com, Hope you like it. Kindly subscribe our feed for any updates. Add me in Facebook and Google+

Posted in Microsoft .Net Help, Software Development