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)}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?