Using Cookies in ASP.NET
Computers & Technology → Internet
- Author Yossi Sigura
- Published November 21, 2010
- Word count 538
Cookie is a small text file that browsers allow sites and Web pages to save on the users' hard disk.
Cookie is an effective and easy way to save user-specific information.
For example, if your site allows visitors to select the background color of a page then it would be a good idea to keep this color on the visitor's hard disk with the help of a cookie, so each time the visitor enters your site you can access the data that you stored in the cookie and change accordingly the background color of the page.
Web sites cookies does not associated with a particular page. This means that you can write cookies from one page
and read the same cookie from another page altogether. Sometimes developers use this to pass data from page to page.
At this point let me urge you not to use Cookies for this purpose. To transfer variables from page to page there are other, more effective, methods. for example by using Query Strings or using Session Variables or global variables.
My recommendation is to Use Cookies to store unique information on visitors computers in order to use it the next time they visit our site, so we will be able to suit them personally.
Please note that there is no limit to the number cookies a single site can create on a visitor's computer.
If you will review the code below you could see I chose to add an expiration date for the cookie I created. It's not a must do to add an expiration date to a cookie but it is a feature commonly used.
When a user revisits a site his browser checks the site's cookies. In case the a cookie has expired the browser does not send the cookie to the server and it deletes it from the visitor's hard disk.
Before we will actually write some code you must know that there is file size limit to cookies and it is about the maximum of 4096 bytes.
Our code to create a new cookie will look like this:
' Create a new cookie and initiate it
Dim NewCookie as httpCookie = New HttpCookie ("Details")
' Add values to the new cookie
New Cookie.Values.Add("Name", "Jon")
New Cookie.Values.Add("LastName", "Doe")
'Add Expiration date to the cookie
newCookie.Expires = #2/7/2010#
' Write the cookie to the visitors hard disk
Response.Cookies.Add(NewCookie)
In the code above I created a cookie and named it "Details". The cookie I created has two values: "first name" and "last name"
I also set an expiration date for the cookie.
The next time the user visits our website we will ask the browser to send us the cookies that we saved on his computer.
The code for reading the cookie Will be:
Dim sName as String = Request.Cookies("Details") ("Name")
Dim sLastName as string = Request.Cookies("Details") ("LastName")
Summary
Use cookies to keep specific information of visitors to your site. Remember that there are limitations to using Cookies.
among other things there is one limitation that can not be overcome and that's the user's ability to prevent cookies to
be written to his hard disk by defining "No cookies allowed" in his browser.
Yossi Sigura is a senior software engineer and a Cloud Computing expert with a passion for programming and programming languages. Yossi writes a few programming blogs among them are: http://www.codeanan.co.il and http://www.devschool.co.il
Article source: https://articlebiz.comRate article
Article comments
There are no posted comments.
Related articles
- What is DuckDuckGo?
- What is CCTLD?
- Gulf Website Hub Reveals Fresh Digital Solutions to Enhance Dubai's Expanding Market.
- Embrace Multi Graphics Inc. Expands Services to Meet Growing Demand in Digital Marketing, Design, and Printing
- Website Development Trends in 2025
- Viewing Instagram Stories Without an Account: Imginn Viewer Insights
- How to Find, Use, and Manage BitLocker Recovery Keys on Windows 10/11
- Building a Professional Website on a Budget: Using Free Tools like WordPress and AI
- Ava Labs CEO On Why You Shouldn't Ignore Red Flags In The Industry
- Cyberbullying: Empowering Families to Safeguard Their Kids
- 10 Common Online Scams to Avoid: Protecting Your Identity and Finances
- Spring Break and Staying Secure Online: An Internet Safety Guide for College Students
- Unveiling the Future: The 10 Revolutionary Trends Shaping Small E-Commerce Businesses in 2024
- Unlocking Online Content with YouTube Video Downloaders
- Unleashing the Potential of Online Earning: A Comprehensive Guide
- Navigating Success in the Digital Realm: Unveiling the Power of Digital Marketing
- How AI Will Affect the Future of Search
- Maximizing Business Efficiency: The Strategic Role of Business Intelligence with DataInseyets
- Cyber Resilience in the Age of AI
- Harnessing the Power of AI & Blockchain for Data Security and Transparency
- AI Ignites 6G Advancements in Wireless Technology
- How AI is Revolutionizing Content Writing
- What You Need to Know About Writing Prompts
- The Remarkable Ways to Use the AI-Powered Chatbot
- Where Will AI Take Us in 2024?
- AI Written Content Creation Trends for 2024
- Will AI-Linked Cryptocurrency Sector Thrive in 2024?
- Is AI Regulation vs AI Deregulation a Real Concern?
- Prompt Engineering: A Beginner's Guide to Prompt Engineering
- Balancing Innovation and Regulation of AI in the Future