如何解决为什么 http.SetCookie() 不添加 cookie?
我试图在登录 cookie 时为用户存储一个 id,但我不明白为什么没有设置 cookie,我添加了 fmt.Println("password matched")
来验证满足条件,但仍然没有设置cookie,我做错了什么?
func login(res http.ResponseWriter,req *http.Request) {
if req.Method == http.MethodPost {
username := req.PostFormValue("username")
password := req.PostFormValue("password")
if data,ok := users[username]; ok {
if data.Password == password {
fmt.Println("password matched")
ssid,_ := uuid.NewV4()
mc := &http.Cookie{
Name: "session",Value: ssid.String(),Path: "/",}
http.SetCookie(res,mc)
tpl.ExecuteTemplate(res,"index.html",username)
} else {
fmt.Fprintln(res,"Incorrect password")
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。