tricks on handling cookies on chrome devTools

本文详细介绍如何在Chrome浏览器中管理网站Cookies,包括查看、添加、删除Cookies的方法,以及使用JavaScript进行Cookies操作的技巧。文章还提供了C#代码示例,用于在服务器端读取和删除Cookies。

I'm currently working with chrome Version 67, the cookie management view changes a lot.  

 

pls refer to the following article for some basic operation

https://developers.google.com/web/tools/chrome-devtools/manage-data/cookies

cookies pane

the trick is, the cookies listed here are ONLY those which are eligible for the current page.  e.g.:  Cookies which has a Path property to some other pages, will not be listed here.  

 

 

tips:  show all cookies for a site:

input the following to address bar

chrome://settings/cookies/detail?site=<your sit>

e.g. chrome://settings/cookies/detail?site=localhost 

-----------------------------------------------------------------------------------------------

 

cookies can also be manipulated on console via javascript:

1. list cookies

document.cookie

 

2. add cookie

  document.cookie="cookieName=value"

cookie with a value of key-value pairs:

   document.cookie = "myCookie=" + JSON.stringify({foo: 'bar', baz: 'poo'});

 

note: if multiple cookies need to be added, call for each

document.cookie="cookieName1=value1"

document.cookie="cookieName2=value2"

 

3. Delete a cookie,

cookie can be delete by setting "expires" ahead of present.

document.cookie = 'cookieName=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'

---------------------------

tricks from chrome:

 all those above operation will be attached with an additional parameter implicitly:

         path=/<currentPath> 

 

if you

a. add cookie on a page

b. go to another page

c. list cookies

the cookie just added will not be shown, coz the path is different

 

best practice on Chrome DevTools

always specify the "path" when handling cookies

 

add: 

       document.cookie="name=value;path=/"

delete:

      document.cookie = 'name=; expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/'

-------------------------------------

C# code to manipulate cookies in controller

a) read cookie

var cookie = Request.Cookies["<name>"];

if(cookie != null)

{

        cookie.Value;//single valued cookie

         cookie.Values["key1"];//valued as key-value pairs

}

 

b) delete cookie

    to delete the cookie from the client side, by adding a expired cookie to response, and then tell the browser to delete it.

string cookieName = "name";

if(Request.Cookies[cookieName ] != null)
{
    HttpCookie myCookie = new HttpCookie(cookieName );
    myCookie.Expires = new DateTime(1970, 1, 1);
    Response.Cookies.Add(myCookie);
}

 

note that, in C# code, when calling new HttpCookie(), the default Path is the root, '/'

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值