2013-06-11 54 views
4

我正在使用Go,大猩猩進行會話和路由以及模板鬍子製作簡單的Web應用程序。我遇到了一個問題,涉及IE瀏覽器接受cookie的問題。該問題只發生在Internet Explorer中,否則登錄在Chrome中完美運行。這裏是我的代碼:在Internet Explorer中進行大猩猩會話

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瀏覽器登錄的用戶被重定向右後衛到登錄頁面,因爲會話值「用戶名」是零,而在Chrome瀏覽器中的用戶名是正確定義和索引頁提供服務。出於某種原因,IE不接受Cookie,但我更改了IE中的所有設置以允許來自任何網站的Cookie。我是否需要更改其中一個cookie選項或向cookie中添加除「MaxAge」以外的其他cookie以接受它?提前致謝。

+0

它會一直很好,如果你想縮短你的代碼,以便它是容易複製。 [我這次爲你做了(或多或少)](http://play.golang.org/p/jvZ5lKqbWE)。 – nemo

+1

對不起,擔心我會發帖太少 - 這是我的第一個SO問題。謝謝你的提示。 – h2ounderthebridge

回答

3

您可能需要在選項中定義cookie的路徑。 下面的選項結構應該做的伎倆:

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

說選項限制cookie的可用性給定的路徑,爲整個 頁面使用"/"

注意,max-age設置not supported by IE

[...]的Internet Explorer(包括IE8)沒有試圖支持Cookie的RFC。 WinINET(IE下面的網絡堆棧)具有基於Cookie的RFC RFC Netscape草案規範的cookie實現。這意味着任何版本的Internet Explorer都不支持像max-age,版本化cookie等命令。

順便說一句,你不需要會話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. 

這應該是所有主要瀏覽器的情況。

+0

這看起來像它的工作。我甚至沒有考慮這個問題,因爲我嘗試了登錄,然後在IE開發人員工具中查看cookie,並認爲它看起來像Path被正確設置。不過,看起來像是明確添加了可用選項的路徑。謝謝您的幫助! – h2ounderthebridge

1

我有一個類似的問題,註銷不能在IE9上使用Gorilla會話(雖然登錄工作正常)。

我最終發現,IE緩存了對我的API端點的響應,並向客戶端發送緩存(304 NOT MODIFIED)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") 
+0

謝謝,這對我很有幫助。添加這些行迫使IE發出新的數據請求,就像其他瀏覽器一樣。 –