Go Gin ctx.SetCookie can't clear cookie for a specific domain - Stack Overflow

I got a strange case when using go Gin, I got a site with this address: I want to clear the cookies to

I got a strange case when using go Gin, I got a site with this address:

I want to clear the cookies to let user logout when user access this path /logout

so in my Gin code I do it like this:

    for _, cookie := range cookies {
        ctx.SetCookie(cookie.Name, "", -1, "/", "opencsg-stg", false, false)
    }

But it's not working, when I check the headers in browser, it looks like this:

When I changed the code to this:

    for _, cookie := range cookies {
        ctx.SetCookie(cookie.Name, "", -1, "/", "", false, false)
    }

it works and the headers in browser is like this:

Not sure why it is working, anyone knows?

I got a strange case when using go Gin, I got a site with this address: https://opencsg-stg

I want to clear the cookies to let user logout when user access this path /logout

so in my Gin code I do it like this:

    for _, cookie := range cookies {
        ctx.SetCookie(cookie.Name, "", -1, "/", "opencsg-stg", false, false)
    }

But it's not working, when I check the headers in browser, it looks like this:

When I changed the code to this:

    for _, cookie := range cookies {
        ctx.SetCookie(cookie.Name, "", -1, "/", "", false, false)
    }

it works and the headers in browser is like this:

Not sure why it is working, anyone knows?

Share Improve this question edited Nov 16, 2024 at 9:28 jub0bs 66.6k27 gold badges195 silver badges196 bronze badges asked Nov 16, 2024 at 3:03 hiveerhiveer 7678 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

TL;DR

Evidence suggests that the cookie(s) you're trying to clear were not created with a Domain attribute. In that case, to effectively clear those cookies, you must set them without specifying any Domain attribute.

More details

Cookies are identified by the following triplet: (name, domain, path). Note that "domain" is tricky: every cookie is associated with a domain, but that doesn't mean it was created with a Domain attribute. Even with all other things being equal, a cookie created with a Domain attribute is different from a cookie created without one. For instance,

Set-Cookie: can-change-username=true; Path=/; Domain=opencsg-stg
Set-Cookie: can-change-username=true; Path=/

creates two distinct cookies in the browser.

Check in your backend code whether those cookies are created with or without a Domain attribute. You can also check this in the browser: the DevTools use a leading . in the value of the Domain column as a visual indicator that a cookie was created with a Domain attribute.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745666161a4639136.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信