英文:
Get existing gorilla sessions
问题
我正在尝试设置一个大猩猩会话,然后再次检索该值。我只是做以下测试。
//创建会话并存储在http Cookies中
session, err := store.Get(req, "session")
if err != nil {
errCode = http.StatusInternalServerError
return
}
//保存一个值
session.Values["user_id"] = userTuple.UserId
err = session.Save(req, w)
if err != nil {
errCode = http.StatusInternalServerError
return
}
//尝试获取刚刚创建的相同会话
nr := http.Request{Header: w.Header()}
session, err = store.Get(&nr, "session")
if err != nil {
errCode = http.StatusInternalServerError
return
} else if session.IsNew {
log.Println("新会话创建而不是旧会话。")
}
这是一个较大的HTTP处理程序的片段。但是相关部分已经发布,第二次调用store.Get()
并没有返回现有的会话,而是返回一个全新的会话。因此,当执行包含此代码的处理程序时,日志语句会打印到控制台。
为什么在这种情况下我会得到一个新的会话,而不是我已经创建和保存的那个会话?
英文:
I am trying to set a gorilla session and then retreive the value again. I am doing the following just as a test.
//create session and store in http Cookies
session, err := store.Get(req, "session")
if err != nil {
errCode = http.StatusInternalServerError
return
}
//save a value
session.Values["user_id"] = userTuple.UserId
err = session.Save(req, w)
if err != nil {
errCode = http.StatusInternalServerError
return
}
//try to get the same session that was just created
nr := http.Request{Header: w.Header()}
session, err = store.Get(&nr, "session")
if err != nil {
errCode = http.StatusInternalServerError
return
} else if session.IsNew {
log.Println("New session created instead of old one.")
}
This is a snippet out of a larger HTTP handler. But the relavent parts are posted and the the second call to store.Get()
is not returning an existing session, but a brand new one. Hence, when the handler this code is in is executed, the log statements is printed to console.
Why am I getting a new session in this case instead of the one I had already created and saved?
答案1
得分: 2
我觉得我已经回答过这个问题了。你在误用这个包。在幕后,context包被用来存储请求的状态,通过创建一个新的、不完整的http.Request,context包返回一个没有会话信息的空状态。
查看这个code来了解我在说什么。
英文:
I feel like I already answered this question. You are misusing this pkg. Behind the scenes the context package is used to store state for a request, by creating a new, incomplete http.Request, the context package returns an empty state with no session info.
check out this code to see what i'm talking about.
答案2
得分: 1
以下代码无法工作,因为实际的季节值保存在写入ResponseWriter
的cookie中:
nr := http.Request{Header: w.Header()}
session, err = store.Get(&nr, "session")
保存的代码在https://github.com/gorilla/sessions/blob/master/store.go#L101中。
而且它基于请求指针存储季节ID,原因很奇怪,请查看https://github.com/gorilla/context/blob/master/context.go#L31。
您需要重新加载页面才能访问新的cookie。
英文:
The following code won't work because the actual season values are saved inside the cookie that is written to ResponseWriter
, copying:
nr := http.Request{Header: w.Header()}
session, err = store.Get(&nr, "session")
The code for saving is https://github.com/gorilla/sessions/blob/master/store.go#L101
And it stores season id based on the request pointer for some odd reason, check https://github.com/gorilla/context/blob/master/context.go#L31.
You'd have to reload the page to access the new cookies.
答案3
得分: 0
我不认为这与gorilla/sessions有关,而是与你构造请求结构有关。
当你调用session.Save(r, w)
时,w.Header()
将包含如下的映射:
map[string][]string{
// ...
"Set-Cookie": []string{/* ... */}
// ...
}
你应该将Set-Cookie
头替换为Cookie
头 - 否则,gorilla/sessions将不会在请求中看到任何cookie,并且会假设请求没有会话cookie。
但是,就像@OneOfOne提到的那样,你通常只需要调用http.Redirect
,让浏览器为你执行Set-Cookie
-Cookie
的替换。
英文:
I don't think this has anything to do with gorilla/sessions, but with how you're constructing that request struct.
When you call session.Save(r, w)
, w.Header()
will now contain a map like the following:
map[string][]string{
// ...
"Set-Cookie": []string{/* ... */}
// ...
}
You should replace that Set-Cookie
header with a Cookie
header - otherwise, gorilla/sessions won't see any cookies in the request, and will assume that the request didn't have a session cookie.
But yeah, like @OneOfOne mentioned, you'd usually just call http.Redirect
and have the browser do that Set-Cookie
-Cookie
replacement for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论