Products
Database Search Solution (New Version) Search Control SEO Pager Highlighter Shortcut Controls Crypt Package Free ASP.NET Controls
Geotargeting Component ASP.NET Media Player Control Flash Video Player Control Services
ASP.NET Telecommute Jobs Free IP Location Lookup Test .Net Regular Expressions CSS/Table/DIV Page Layouts Custom Programming Article Sites Master List |
Cookies in ASP.NETWhat is a cookie?Cookie are one of several ways to store data about web site visitors during the time when web server and browser are not connected. Common use of cookies is to remember users between visits. Practically, cookie is a small text file sent by web server and saved by web browser on client machine. For example, when visitor comes to your web site you can store information about last visit and retrieve that information when visitor comes next time. How to create a cookie in ASP.NETTo write a cookie in ASP.NET we can use a code like this: [ VB.NET ] ' Add
this on the beginning of your .vb code file ' To add
multiple key/value pairs in single cookie [ C# ] // Add
this on the beginning of your .vb code file // To
add multiple key/value pairs in single cookie How to read a cookie in ASP.NETTo read a cookie value, use this: [ VB.NET ] Dim
MyCookieValue As String [ C# ] string
MyCookieValue; How to delete cookie in ASP.NETTo delete existing cookie we actually just set its expiration time to some time in the past. You can do it with code like this: [ VB.NET ] ' First
check if cookie exists [ C# ] // First
check if cookie exists HttpCookie classHttpCookie class is located in System.Web namespace. You can use HttpCookie class to create and manipulate cookies instead of using of Response and Request objects. HttpCookie class have these properties: You can use HttpCookie class to create a cookie or set cookie's properties, like in this example code: [ VB.NET ] Dim
MyGreatCookie As HttpCookie =
New HttpCookie("MyCookieName") [ C# ]
HttpCookie
MyGreatCookie = new
HttpCookie("MyCookieName"); Web browser limits for cookiesCookie size is limited to 4096 bytes. It is not much, so cookies are used to store small amounts of data, often just user id. Also, number of cookies is limited to 20 per website. If you make new cookie when you already have 20 cookies, browser will delete oldest one. Your web site visitor can change browser settings to not accept cookies. In that case you are not able to save and retrieve data on this way! Because of this, it is good to check browser settings before saving a cookie. If your visitor blocked cookies in web browser privacy settings, you need to decide do you still want to save that data on some other way (maybe with sessions) or to not save it at all. Anyway, you application must continue to work normally with any browser privacy settings. It is better to not store any sensitive or critical data to cookies. If using of cookies is necessary, you should inform your users with some message like: "Cookies must be enabled to use this application". How to find does web browser accepts cookiesThere are two possible cases when your client will not accept cookies: - Web browser does not support cookies How to check does visitor's web browser supports cookies[ VB.NET ] If
Request.Browser.Cookies Then [ C# ] if
(Request.Browser.Cookies) How to check if client web browser not saved a cookie because of its privacy settingsCode above will tell you does web browser supports cookie technology, but your visitor could disable cookies in web browser's privacy settings. In that case, Request.Browser.Cookies will still return true but your cookies will not be saved. Only way to check client's privacy settings is to try to save a cookie on the first page, and then redirect to second page that will try to read that cookie. You can eventually use the same page to save and read a cookie when perform a testing, but you must use Response.Redirect method after saving and before reading cookies. Best practices with cookies in ASP.NETCookies are just plain text, so usually are not used to store sensitive informations like passwords without prior encryption. If you want to enable "Remember me" option on web site it is recommended to encrypt a password before it is stored in a cookie. Cookies are often used for data like: when visitor last time loged in, what site color she likes, to keep referer id if we offer affiliate program etc. Security issues about cookies in ASP.NETBecause of security reasons, your web application can read only cookies related to your web domain. You can't read cookies related to other web sites. Web browser stores cookies from different sites separately. Cookie is just a plain text file on client's hard disk so it could be changed on different ways outside of your application. Because of that, you need to treat cookie value as potentially dengerous input like any other input from the visitor, including prevention of cross site scripting attacks. Tutorial toolbar: Tell A Friend | Add to favorites | Feedback | Google |