Checking / Reading / Writing Cookies in .NET, please help me understand

Therefore, I never had to use cookies, but now I am creating a shopping basket that they can continue to return and leave, but I want it to keep what they added.

What interests me:

How to check if a cookie exists and then create or update it, is this the best way to think about using a cookie?

How exactly will I store the data, in particular, I want to save a list of identifiers like "5,6,7,8", should I just use one line for this, or is there a faster / better way than reading / parsing / writing something like that? I mean, I suppose that I will just add new_value + ',' to the end, is there an append for cookie variables?

Does the cookie have a unique identifier that I would use to make sure that I am not writing duplicates or anything else?

Note. It's easy to see โ€œHOWโ€, how to see the syntax, but I'm really trying to understand the โ€œbest wayโ€ or the most ideal, how it should have been used, or like all you programmers have found the most fruitful way to use them in this type of script.

+6
c # cookies
source share
3 answers

The answer to this similar question suggests that you only store the user ID in a cookie. The rest is sent to the database.

If you can consider other approaches besides cookies, many people prefer to use a session using cookies. Firstly, you donโ€™t always have a lot of space in a cookie.

Storing a shopping basket in a cookie means that you will not have a record of what people buy but have not bought.

OTOH, using a cookie, uses customer storage space and saves your own. This can be significant over time and a large number of buyers.

+1
source share

I solved this in the past by creating a class for managing cookies (like CookieManager ) with static methods with which I passed an HttpRequest object.

I tried to solve a very similar problem, so I created a Count cookie, and then a cookie, which contained the information that I wanted to store (in your case, an identification number). I just wanted to save the last 5 items that the user was viewing, so I would deal with this in my CookieManager class by removing the oldest cookie and queuing up last. The Count cookie kept track of how many cookies I had. Obviously, this is not very high-tech or safe, but this project is completely unnecessary. All that you want to be reliable must be stored in a database or elsewhere on the server side.

0
source share

I want to explain again why you only store the manual that maps to the user ID in the cookie. There are two main reasons:

  • Performance. As slowly as it might seem to retrieve data from a database, you should remember that cookie data is not free. It should be downloaded from the user's browser to your web server, and even high-speed broadband connections have a much slower download speed. On the contrary, your database most likely has a gigabit link (sometimes even faster) directly on the web server. So what you really want in your cookie for better performance is a guide that maps directly to the primary key of your database table.
  • Security. The data in cookies is stored in a text file on the user's computer. You never know where the user will get access to your site; it can be a very public place that is not suitable for storing such data.

So, is there any data with which you can directly use cookies? As it happens, there is. Cookies have excellent adherence to a specific machine and browser. These days, many people will access the Internet from several places. Perhaps a working computer, home computer, smartphone, netbook ... all of which may have different screen sizes and other features. So what you can do with the cookie is the repository information specific to this combination of user + location.

0
source share

All Articles