drbxr86044 2015-12-01 16:25
浏览 173
已采纳

Golang-无法使Cookie过期

I'm currently playing with the Go App Engine SDK and I'm trying to set/expires Cookies.

There is no problem by setting a Cookie, but it is impossible to make it expiring in the browser.

The app is a based on a negroni instance:

func init() {

    app := negroni.New()
    app.UseHandler(Router())
    http.Handle("/", app)

}

The router is a mux instance:

func Router() *mux.Router {

    r := mux.NewRouter()
    subRouter := r.PathPrefix(PATH_PREFIX).Subrouter()

    subRouter.HandleFunc("/sign", LoginHandler)
    subRouter.HandleFunc("/userinfo", UserInfo)
    subRouter.HandleFunc("/logout", Logout)

    return r
}

The login handler is basic:

func LoginHandler(w http.ResponseWriter, r *http.Request) {

    ctx := appengine.NewContext(r)

    u := user.Current(ctx)

    if u == nil {

        url, err := user.LoginURL(ctx, r.URL.String())
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        w.Header().Set("Location", url)
        w.WriteHeader(http.StatusFound)
        return
    }

    //COOKIE_ID = "SomeString"
    cookie := &http.Cookie{Name: COOKIE_ID, Value: u.ID, Path: "/", MaxAge: 0}
    http.SetCookie(w, cookie)

    w.Header().Set("Location", "/")
    w.WriteHeader(http.StatusFound)
}

To expires the cookie:

func Logout(w http.ResponseWriter, r *http.Request) {

    ctx := appengine.NewContext(r)
    url, err := user.LogoutURL(ctx, "/")

    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    expiredCookie := &http.Cookie{Name: COOKIE_ID, MaxAge: -10, Expires: time.Now()}
    http.SetCookie(w, expiredCookie)

    w.Header().Set("Location", url)
    w.WriteHeader(http.StatusFound)
}

I tried everything:

  • Getting the old cookie, change MaxAge and Expires
  • Create a new cookie with the same Name to overwrite

Full code: https://gist.github.com/yageek/78e43c83b56467fc8338

In any case, the cookie still remains in the navigator. What do I do wrong ?

  • 写回答

1条回答 默认 最新

  • dongqiya9552 2015-12-01 16:40
    关注

    Browsers store cookies for multiple paths. Set the path attribute on the cookie header to match the path attribute used to create the cookie.

    Set maximum age to a negative value to clear the cookie. The maximum age attribute is not set on the header if equal to zero.

    Some browsers do not understand the maximum age attribute. Set expiration to a time in the past for the benefit of those browsers.

    expiredCookie := &http.Cookie{Path: "/", Name: COOKIE_ID, MaxAge: -1, Expires: time.Now().Add(-100 * time.Hour)}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率
  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)