Cookies with More Than One Value in asp.net
You can store one value in a cookie, such as user name or last visit. You can also store multiple name-value pairs in a single cookie. The name-value pairs are referred to as subkeys. For example, instead of creating two separate cookies named userName and lastVisit, you can create a single cookie named userInfo that has the subkeys userName and lastVisit.
You might use subkeys for several reasons. First, it is convenient to put related or similar information into a single cookie. In addition, because all the information is in a single cookie, cookie attributes such as expiration apply to all the information.
A cookie with subkeys also helps you limit the size of cookie files. As noted earlier in the "Cookie Limitations" section, cookies are usually limited to 4096 bytes and you can't store more than 20 cookies per site. By using a single cookie with subkeys, you use fewer of those 20 cookies that your site is allotted. In addition, a single cookie takes up about 50 characters for overhead (expiration information, and so on), plus the length of the value that you store in it, all of which counts toward the 4096-byte limit. If you store five subkeys instead of five separate cookies, you save the overhead of the separate cookies and can save around 200 bytes.
To create a cookie with subkeys, you can use a variation of the syntax for writing a single cookie. The following example shows two ways to write the same cookie, each with two subkeys:
Response.Cookies["userInfo"]["userName"] = "patrick";
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);
HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Values["userName"] = "patrick";
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
Recent Posts
- What is partial classess in .net?
- What is Fragment Caching in ASP.NET?
- Interview Questions: What are Properties in .NET, What are the advantages of using Properties in .NET, How to declare a property in .NET?
- Will You Lower Your Salary Expectations
- Interview Questions on Networking, Part 2
- Interview Questions on Networking
- Software Testing – Quality Assurance Interview Questions
- Interview Questions on Software Testing
- Note, A Simple ‘Thank You’ Can Land the Job
- Define Trace Switches
Tag Cloud
-
application
ASP
assembly
class
client
code
company
constructor
control
Database
Debug
difference
Don
employer
hiring manager
information
interview
interviewer
job
job seekers
language
manager
method
NET
page
person
position
Process
question
quot
response
salary
Serialization
Server
State
string
system
table
time
type
user
way
web
work
XML