tricks on handling cookies on chrome devTools

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, '/'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值