Go、セッションとルーティング用のゴリラ、テンプレート用の口ひげを使用して、単純な Web アプリを作成しています。ログインに問題があり、IE が Cookie を受け入れることに問題があると思います。この問題は Internet Explorer でのみ発生しますが、それ以外の場合、ログインは 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 を受け入れていませんが、すべてのサイトからの Cookie を許可するように IE のすべての設定を変更しました。IE が受け入れるには、Cookie オプションの 1 つを変更するか、「MaxAge」以外の何かを Cookie に追加する必要がありますか? 前もって感謝します。