我正在做一个简单的web应用程序,使用Go,gorilla用于会话和路由,mustache用于模板。我在登录时遇到了一个问题,我相信是IE接受cookie的问题。这个问题只在in浏览器上出现,但在Chrome中登录就可以完美地工作了。下面是我的代码:
func main() {
r := mux.NewRouter()
r.HandleFunc("/performance", Index)
r.HandleFunc("/performance/login", Login)
log.Fatal(http.ListenAndServe(":5901", r))
}
func Index(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "performance")
if session.Values["username"] == nil {
http.Redirect(w, r, "/performance/login", http.StatusSeeOther)
}
dict := session.Values
fmt.Fprintf(w, mustache.RenderFileInLayout("templates/index.html", "templates/basepage.html", dict))
}
func Login(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
results := 0
r.ParseForm()
u := r.FormValue("username")
pass := r.FormValue("password")
p := PassEncrypt(pass)
q := map[string]string{}
rows, err := db.Query("SELECT username, name, title FROM user WHERE (username=$1) AND (password=$2)", u, p)
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var username string
var name string
var title string
if err := rows.Scan(&username, &name, &title); err != nil {
log.Fatal(err)
}
q["username"] = username
q["name"] = name
q["title"] = title
results++
}
if results > 0 {
session, _ := store.Get(r, "performance")
session.Options = &sessions.Options{
MaxAge: 900,
}
session.Values["username"] = q["username"]
session.Values["name"] = q["name"]
session.Values["title"] = q["title"]
session.Save(r, w)
http.Redirect(w, r, "/performance", http.StatusSeeOther)
} else {
http.Redirect(w, r, "/performance/login", http.StatusSeeOther)
}
} else {
fmt.Fprintf(w, mustache.RenderFileInLayout("templates/login.html", "templates/basepage.html", nil))
}
}当使用IE登录时,用户会被重定向回登录页面,因为会话值" username“为nil,而在Chrome中,用户名是正确定义的,并且提供了索引页面。由于某些原因,IE不接受cookie,但我更改了IE中的所有设置,以允许来自任何站点的cookie。IE是否需要更改cookie选项之一或向cookie中添加"MaxAge“以外的其他内容才能接受它?提前谢谢。
发布于 2013-06-12 07:54:51
您可能需要在选项中定义cookie的路径。下面的options结构应该可以做到这一点:
session.Options = &sessions.Options{
Path: "/performance",
}对于使用"/"的整个页面,所述选项将cookie的可用性限制到给定的路径。
请注意,max-age设置为not supported by IE
...Internet Explorer (包括IE8)不会尝试支持任何用于cookies的RFC。WinINET (IE下面的网络堆栈)具有基于RFC Netscape之前的cookie规范草案的cookie实现。这意味着任何版本的Internet Explorer都不支持max-age、versioned cookies等指令。
顺便说一句,您不需要会话cookie(来自IE manual on cookies)的MaxAge:
(expires=date;)
If you set no expiration date on a cookie, it expires when the browser
closes. If you set an expiration date, the cookie is saved across browser
sessions. If you set an expiration date in the past, the cookie is deleted.
Use Greenwich Mean Time (GMT) format to specify the date.这应该是所有主流浏览器的情况。
发布于 2015-06-04 01:06:18
我遇到了一个类似的问题,在使用Gorilla会话的情况下,注销不能在IE9上工作(尽管登录可以正常工作)。
我最终发现,IE正在缓存对我的API端点的响应,并将缓存的(304未修改) API响应发送到客户端,即使Cookie值正在更改。
强制从不缓存API终结点解决了此问题:
w.Header().Set("Expires", "Tue, 03 Jul 2001 06:00 GMT")
w.Header().Set("Last-Modified", "{now} GMT")
w.Header().Set("Cache-Control", "max-age=0, no-cache, must-revalidate, proxy-revalidate")https://stackoverflow.com/questions/17053699
复制相似问题