首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Internet Explorer中的Go和gorilla会话

Internet Explorer中的Go和gorilla会话
EN

Stack Overflow用户
提问于 2013-06-12 04:57:52
回答 2查看 975关注 0票数 4

我正在做一个简单的web应用程序,使用Go,gorilla用于会话和路由,mustache用于模板。我在登录时遇到了一个问题,我相信是IE接受cookie的问题。这个问题只在in浏览器上出现,但在Chrome中登录就可以完美地工作了。下面是我的代码:

代码语言:javascript
复制
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“以外的其他内容才能接受它?提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-12 07:54:51

您可能需要在选项中定义cookie的路径。下面的options结构应该可以做到这一点:

代码语言:javascript
复制
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

代码语言:javascript
复制
(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.

这应该是所有主流浏览器的情况。

票数 3
EN

Stack Overflow用户

发布于 2015-06-04 01:06:18

我遇到了一个类似的问题,在使用Gorilla会话的情况下,注销不能在IE9上工作(尽管登录可以正常工作)。

我最终发现,IE正在缓存对我的API端点的响应,并将缓存的(304未修改) API响应发送到客户端,即使Cookie值正在更改。

强制从不缓存API终结点解决了此问题:

代码语言:javascript
复制
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")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17053699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档