zoukankan      html  css  js  c++  java
  • Using HTTP cookies

    Using HTTP cookies

       An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to the user's web browser. The browser may store it and send it back with later requests to the same server. Typically, it is used to tell if two requests came from the same browser — keeping a user logged-in, for example. It remembers stateful information for the stateless HTTP protocol.

    Cookies are mainly used for three purposes:

    Session management

    Logins, shopping carts, game scores, or anything else the server should remember

    Personalization

    User preferences, themes, and other settings

    Tracking

    Recording and analyzing user behavior

    Cookies were once used for general client-side storage. While this was legitimate when they were the only way to store data on the client, it is now recommended to use modern storage APIs. Cookies are sent with every request, so they can worsen performance (especially for mobile data connections). Modern APIs for client storage are the Web Storage API (localStorage and sessionStorage) and IndexedDB.

    Note: To see stored cookies (and other storage that a web page can use), you can enable the Storage Inspector in Developer Tools and select Cookies from the storage tree.

    Restrict access to cookies

    There are a couple of ways to ensure that cookies are sent securely and are not accessed by unintended parties or scripts: the Secure attribute and the HttpOnly attribute.

    A cookie with the Secure attribute is sent to the server only with an encrypted request over the HTTPS protocol, never with unsecured HTTP (except on localhost), and therefore can't easily be accessed by a man-in-the-middle attacker. Insecure sites (with http: in the URL) can't set cookies with the Secure attribute. However, do not assume that Secure prevents all access to sensitive information in cookies; for example, it can be read and modified by someone with access to the client's hard disk (or JavaScript if the HttpOnly attribute is not set).

    A cookie with the HttpOnly attribute is inaccessible to the JavaScript Document.cookie API; it is sent only to the server. For example, cookies that persist server-side sessions don't need to be available to JavaScript, and should have the HttpOnly attribute. This precaution helps mitigate cross-site scripting (XSS) attacks.

    Here is an example:

    Set-Cookie: id=a3fWa; Expires=Thu, 21 Oct 2021 07:28:00 GMT; Secure; HttpOnly
    

    127.0.0.1和localhost都不会受到secure的影响,但是局域网ip是会受到影响,比如172.16.210.99。服务器不会通过http下发secure的cookie。

     但是微软的FormsAuthentication,对localhost和127.0.0.1有点特殊,虽然cookie正常下发。在request的时候,也会附带上forms authentication的cookie,但是asp.net本身不会去解析对应的cookie.

  • 相关阅读:
    2020春软件工程助教工作总结【第十四周】
    【西北师大-20软工】第三次团队作业成绩汇总
    2020春软件工程助教工作总结【第十二周】
    2020春软件工程助教工作总结【第十周】
    将作业提交到班级博客的一些注意事项
    操作系统第6次实验报告:使用信号量解决进程互斥访问
    操作系统第5次实验报告:内存管理
    操作系统第4次实验报告:文件系统
    操作系统第3次实验报告:管道
    操作系统第2次实验报告:创建进程
  • 原文地址:https://www.cnblogs.com/chucklu/p/15237653.html
Copyright © 2011-2022 走看看