本文整理汇总了Golang中github.com/astaxie/beego/context.Context.SetCookie方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.SetCookie方法的具体用法?Golang Context.SetCookie怎么用?Golang Context.SetCookie使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/astaxie/beego/context.Context
的用法示例。
在下文中一共展示了Context.SetCookie方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetLoginRedirect
// get login redirect url from cookie
func GetLoginRedirect(ctx *context.Context) string {
loginRedirect := strings.TrimSpace(ctx.GetCookie("login_to"))
if utils.IsMatchHost(loginRedirect) == false {
loginRedirect = "/"
} else {
ctx.SetCookie("login_to", "", -1, "/")
}
return loginRedirect
}
示例2: giveCookie
func giveCookie(ctx *context.Context) {
_, err := ctx.Request.Cookie("uid")
if err != nil {
//没有cookie
uid := GetID()
h := md5.New()
h.Write([]byte(uid))
cookie := hex.EncodeToString(h.Sum(nil))
err = models.AddUser(uid, cookie)
if err != nil {
return
}
ctx.SetCookie("uid", cookie, 1<<31-1)
beego.Info(cookie)
}
return
}
示例3: readFlash
// readFlash is similar to beego.ReadFromRequest except that it takes a *context.Context instead
// of a *beego.Controller, and returns a map[string]string directly instead of a Beego.FlashData
// (which only has a Data field anyway).
func readFlash(ctx *context.Context) map[string]string {
data := map[string]string{}
if cookie, err := ctx.Request.Cookie(beego.FlashName); err == nil {
v, _ := url.QueryUnescape(cookie.Value)
vals := strings.Split(v, "\x00")
for _, v := range vals {
if len(v) > 0 {
kv := strings.Split(v, "\x23"+beego.FlashSeperator+"\x23")
if len(kv) == 2 {
data[kv[0]] = kv[1]
}
}
}
// read one time then delete it
ctx.SetCookie(beego.FlashName, "", -1, "/")
}
return data
}
示例4: SetLangVer
// SetLangVer sets site language version.
// It returns true when language is changed by URL parameters.
func SetLangVer(ctx *context.Context, input url.Values, data map[interface{}]interface{}) (bool, LangType) {
isNeedRedir := false
// 1. Check URL arguments.
lang := input.Get("lang")
// 2. Get language information from cookies.
if len(lang) == 0 {
ck, err := ctx.Request.Cookie("lang")
if err == nil {
lang = ck.Value
}
} else {
isNeedRedir = true
}
// Check again in case someone modify by purpose.
isValid := false
for _, v := range LangTypes {
if lang == v.Lang {
isValid = true
break
}
}
if !isValid {
lang = ""
isNeedRedir = false
}
// 3. Get language information from 'Accept-Language'.
if len(lang) == 0 {
al := ctx.Request.Header.Get("Accept-Language")
if len(al) > 2 {
al = al[:2] // Only compare first two letters.
for _, v := range LangTypes {
if al == v.Lang {
lang = al
break
}
}
}
}
// 4. DefaucurLang language is English.
if len(lang) == 0 {
lang = "en-US"
isNeedRedir = false
}
curLang := LangType{
Lang: lang,
}
// Save language information in cookies.
ctx.SetCookie("lang", curLang.Lang, 1<<31-1, "/")
restLangs := make([]*LangType, 0, len(LangTypes)-1)
for _, v := range LangTypes {
if lang != v.Lang {
restLangs = append(restLangs, v)
} else {
curLang.Name = v.Name
}
}
// Set language properties.
data["Lang"] = curLang.Lang
data["CurLang"] = curLang.Name
data["RestLangs"] = restLangs
return isNeedRedir, curLang
}
示例5: DeleteRememberCookie
func DeleteRememberCookie(ctx *context.Context) {
ctx.SetCookie(setting.CookieUserName, "", -1)
ctx.SetCookie(setting.CookieRememberName, "", -1)
}
示例6: WriteRememberCookie
func WriteRememberCookie(user *models.User, ctx *context.Context) {
secret := utils.EncodeMd5(user.Rands + user.Password)
days := 86400 * setting.LoginRememberDays
ctx.SetCookie(setting.CookieUserName, user.UserName, days)
ctx.SetSecureCookie(secret, setting.CookieRememberName, user.UserName, days)
}
示例7: DeleteRememberCookie
func (this *BaseController) DeleteRememberCookie(ctx *context.Context) {
ctx.SetCookie(setting.CookieUsername, "", -1)
ctx.SetCookie(setting.CookieRememberName, "", -1)
}
示例8: WriteRememberCookie
func (this *BaseController) WriteRememberCookie(u *user.User, ctx *context.Context) {
secret := utils.EncodeMd5(u.PasswordSalt + u.Password)
days := 86400 * setting.LoginRememberDays
ctx.SetCookie(setting.CookieUsername, u.Username, days)
ctx.SetSecureCookie(secret, setting.CookieRememberName, u.Username, days)
}
示例9: SetCookie
func SetCookie(ctx *context.Context, username, password string, expire int) {
ctx.SetCookie("uname", username, expire, "/")
ctx.SetCookie("pwd", password, expire, "/")
}