在Internet Explorer中执行Go和gorilla会话 [英] Go and gorilla sessions in Internet Explorer

查看:192
本文介绍了在Internet Explorer中执行Go和gorilla会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Go,gorilla进行会话和路由,以及使用模板创建一个简单的网络应用程序。我有一个与登录有关的问题,我相信,IE接受cookie的问题。该问题只会发生在Internet Explorer,否则登录工作完美的Chrome。这是我的代码:

I'm making a simple web app using Go, gorilla for sessions and routing, and mustache for templates. I'm having an issue with the login involving, I believe, a problem with IE accepting the cookie. The problem only occurs with Internet Explorer, but otherwise the login works perfectly in Chrome. Here is my code:

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。我需要更改其中一个cookie选项或添加一些东西到除了MaxAge的IE以接受它吗?提前感谢。

When logging in using IE the user is redirected right back to the login page because the session value "username" is nil, while in Chrome the username is correctly defined and the index page is served. For some reason IE is not accepting the cookie, yet I changed all settings in IE to allow cookies from any site. Do I need to change one of the cookie options or add something to the cookie other than "MaxAge" for IE to accept it? Thanks in advance.

推荐答案

您可能需要在选项中定义Cookie的路径。
以下选项struct应该可以做到:

You probably need to define the cookie's path in your options. The following options struct should do the trick:

session.Options = &sessions.Options{
    Path: "/performance",
}



所述选项将Cookie的可用性限制为给定路径,对于整个页面
使用/

$ c> max-age 设置为不支持IE

Note that the max-age setting is not supported by IE:


[...] IE8)不尝试支持任何Cookie的Cookie。
WinINET(IE下的网络栈)具有基于cookie的Netscape草案规范的cookie实现。这意味着任何版本的Internet Explorer都不支持max-age,版本化Cookie等指令。

[...] Internet Explorer (including IE8) does not attempt to support any RFC for cookies. WinINET (the network stack below IE) has cookie implementation based on the pre-RFC Netscape draft spec for cookies. This means that directives like max-age, versioned cookies, etc, are not supported in any version of Internet Explorer.

顺便说一下,您不需要会话Cookie的 MaxAge (来自关于Cookie的IE手册):

By the way, you don't need a MaxAge for session cookies (from the IE manual on cookies):

(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.

所有主流浏览器都应如此。

This should be the case for all major browsers.

这篇关于在Internet Explorer中执行Go和gorilla会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆