dongpao5127 2015-08-06 21:06
浏览 75
已采纳

Golang:检索大猩猩/会话Cookie

I'm trying to find out the cookie value that is being saved to the clients browser via s.Save(r, w). That way I can store the cookie value in a database before calling renderTemplate(). Is there a way to accomplish this using the gorilla/sessions api?

func login(w http.ResponseWriter, r *http.Request, db *sql.DB, store *sessions.CookieStore, t *template.Template){
  if r.Method == "POST" {
    r.ParseForm()
    username, password, remember := r.FormValue("user[name]"), r.FormValue("user[password]"), r.FormValue("remember_me")
    User, err := users.Login(db, username, password, remember, r.RemoteAddr)
    if err != nil {
        http.Error(w, err.Error(), 500)
        return
    }
    s, _ := store.Get(r, "rp-session")  //returns a new session
    s.Save(r, w)
    //fmt.Println(s.Values)
    renderTemplate(w, "user_nav_info", t, User)
  } 
}
  • 写回答

1条回答 默认 最新

  • dongmiyi8220 2015-08-06 21:58
    关注

    This is how gorilla's sessions api saves the session - https://github.com/gorilla/sessions/blob/master/store.go#L104-L109

    So, you can essentially include the below code to get the exact cookie value being set in client's machine.

    encoded, err := securecookie.EncodeMulti(s.Name(), s.Values, store.Codecs...)
    if err != nil {
        return err
    }
    cookieVal := NewCookie(s.Name(), encoded, s.Options)
    fmt.Println(cookieVal) // cookie value that is sent to client
    

    Although, I'd not suggest you to save the cookie that way. gorilla's sessions api is supposed to the job for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序