當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Cookie.String方法代碼示例

本文整理匯總了Golang中net/http.Cookie.String方法的典型用法代碼示例。如果您正苦於以下問題:Golang Cookie.String方法的具體用法?Golang Cookie.String怎麽用?Golang Cookie.String使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net/http.Cookie的用法示例。


在下文中一共展示了Cookie.String方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: getSession

// Get the session from the cookie in the request
// Creates a new session and cookie if none can be found
func getSession(w http.ResponseWriter, req *http.Request) (*Session, bool) {

	cookie, err := req.Cookie("id")
	if err == nil {
		s, ok := sessionSync(cookie.Value)
		if ok {
			return s, ok
		}

		log.Println("Invalid session cookie presented: ", cookie.Value)
	}

	// Create a session
	s := newSession()
	config.Debug("Creating new session ", s.id)
	sessions.Create(s)

	// Write the session to the HTTP response
	newcookie := http.Cookie{
		Name:     "id",
		Value:    s.id,
		Path:     "/",
		MaxAge:   math.MaxInt32,
		HttpOnly: true}
	w.Header().Add("Set-Cookie", newcookie.String())

	s.mutex.Lock()
	return s, true
}
開發者ID:alienscience,項目名稱:bread,代碼行數:31,代碼來源:cookie.go

示例2: login

//User login
func (uc UsersController) login(request *restful.Request,
	response *restful.Response) {
	loginCredentials := new(UserLoginCredentials)
	err := request.ReadEntity(loginCredentials)
	if err != nil {
		LogError(request, response, err)
		WriteIllegalRequestError(response)
		return
	}
	cookieAuth, err := new(UserManager).Login(loginCredentials)
	if err != nil {
		LogError(request, response, err)
		WriteError(err, response)
		return
	}
	//Create an Auth cookie
	authCookie := http.Cookie{
		Name:     "AuthSession",
		Value:    cookieAuth.AuthToken,
		Path:     "/",
		HttpOnly: true,
	}
	//Create a CSRF cookie for this session
	//Subsequent requests must include this in a header field
	//X-Csrf-Token
	csrfCookie := http.Cookie{
		Name:     "CsrfToken",
		Value:    util.GenHashString(cookieAuth.AuthToken),
		Path:     "/",
		HttpOnly: false,
	}
	response.AddHeader("Set-Cookie", authCookie.String())
	response.AddHeader("Set-Cookie", csrfCookie.String())
	response.WriteEntity(BooleanResponse{Success: true})
}
開發者ID:firebitsbr,項目名稱:wikifeat,代碼行數:36,代碼來源:user_controller.go

示例3: login

func (this *loginController) login(w http.ResponseWriter, req *http.Request) {
	w.Header().Add("Content-Type", "text/html")
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	if req.Method == "POST" {
		email := req.FormValue("email")
		password := req.FormValue("password")

		member, err := models.GetMember(email, password)
		if err != nil {
			responseWriter.Write([]byte(err.Error()))
			return
		}

		session, err := models.CreateSession(member)
		if err != nil {
			responseWriter.Write([]byte(err.Error()))
			return
		}

		var cookie http.Cookie
		cookie.Name = "sessionId"
		cookie.Value = session.SessionId()
		responseWriter.Header().Add("Set-Cookie", cookie.String())
	}
	vm := viewmodels.GetLogin()
	this.template.Execute(responseWriter, vm)
}
開發者ID:obadonke,項目名稱:psGoWebApp,代碼行數:29,代碼來源:login.go

示例4: logout

//User logout
func (uc UsersController) logout(request *restful.Request,
	response *restful.Response) {
	token := func() string {
		for _, cookie := range request.Request.Cookies() {
			if cookie.Name == "AuthSession" {
				return cookie.Value
			}
		}
		return ""
	}()
	err := new(UserManager).Logout(token)
	if err != nil {
		LogError(request, response, err)
		WriteError(err, response)
		return
	}
	//Because CouchDB doesn't actually destroy the session,
	//best we can do is clear the cookie in the browser.
	//This is apparently "not a bug" :|
	//http://webmail.dev411.com/t/couchdb/dev/141xwf5vb0/jira-created-couchdb-2042-session-not-cleared-after-delete-session-cookie-auth
	theCookie := http.Cookie{
		Name:     "AuthSession",
		Value:    "",
		Path:     "/",
		HttpOnly: true,
	}
	response.AddHeader("Set-Cookie", theCookie.String())
	response.WriteEntity(BooleanResponse{Success: true})
}
開發者ID:firebitsbr,項目名稱:wikifeat,代碼行數:30,代碼來源:user_controller.go

示例5: login

func (h *homeController) login(w http.ResponseWriter, req *http.Request) {
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	w.Header().Add("Content Type", "text/html")

	if req.Method == "POST" {
		email := req.FormValue("email")
		password := req.FormValue("password")

		member, err := models.GetMember(email, password)

		if err == nil {
			session, err := models.CreateSession(member)
			log.Printf("create session err: %v", err)
			if err == nil {
				var cookie http.Cookie
				cookie.Name = "sessionId"
				cookie.Value = session.SessionId()
				responseWriter.Header().Add("Set-Cookie", cookie.String())
			}
		} else {
			log.Print("User not found!")
		}
	}

	vm := viewmodels.GetLogin()

	h.loginTemplate.Execute(responseWriter, vm)
}
開發者ID:Partyschaum,項目名稱:go-web-app,代碼行數:30,代碼來源:home.go

示例6: commitSession

func commitSession(headers Headers, env Env, key, secret, domain string) {
	cookie := new(http.Cookie)
	cookie.Name = key
	cookie.Value = encodeCookie(env["mango.session"].(map[string]interface{}), secret)
	cookie.Domain = domain
	headers.Add("Set-Cookie", cookie.String())
}
開發者ID:senica,項目名稱:mango,代碼行數:7,代碼來源:sessions.go

示例7: setStickyCookieBackend

func setStickyCookieBackend(res *http.Response, backend string, cookieKey [32]byte) {
	cookie := http.Cookie{
		Name:  stickyCookie,
		Value: base64.StdEncoding.EncodeToString(encrypt([]byte(backend), cookieKey)),
		Path:  "/",
	}
	res.Header.Add("Set-Cookie", cookie.String())
}
開發者ID:ably-forks,項目名稱:flynn,代碼行數:8,代碼來源:transport.go

示例8: DeleteCookie

func (s *Session) DeleteCookie(options cookieOptions, response http.ResponseWriter) {
	cookie := http.Cookie{
		Name:   options.Name,
		Value:  "-",
		MaxAge: -1,
	}

	response.Header().Set("Set-Cookie", cookie.String())
}
開發者ID:xrstf,項目名稱:raziel,代碼行數:9,代碼來源:session.go

示例9: clearAllCookies

func clearAllCookies(rw http.ResponseWriter, req *http.Request) {
	for _, ck := range req.Cookies() {
		ck2 := http.Cookie{
			Name:   ck.Name,
			MaxAge: -1,
		}
		rw.Header().Add("Set-Cookie", ck2.String())
	}
}
開發者ID:JeremyRand,項目名稱:ncdns,代碼行數:9,代碼來源:web.go

示例10: AddCsrfCookie

func AddCsrfCookie(rw http.ResponseWriter, sessToken string) {
	csrfCookie := http.Cookie{
		Name:     "CsrfToken",
		Value:    util.GenHashString(sessToken),
		Path:     "/",
		HttpOnly: false,
	}
	rw.Header().Add("Set-Cookie", csrfCookie.String())
}
開發者ID:rhinoman,項目名稱:wikifeat,代碼行數:9,代碼來源:auth.go

示例11: SetCookie

// set cookie
// args: name, value, max age, path, domain, secure, http only, expires
func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
	cookie := http.Cookie{}
	cookie.Name = name
	cookie.Value = url.QueryEscape(value)

	if len(others) > 0 {
		switch v := others[0].(type) {
		case int:
			cookie.MaxAge = v
		case int64:
			cookie.MaxAge = int(v)
		case int32:
			cookie.MaxAge = int(v)
		}
	}

	cookie.Path = "/"
	if len(others) > 1 {
		if v, ok := others[1].(string); ok && len(v) > 0 {
			cookie.Path = v
		}
	}

	if len(others) > 2 {
		if v, ok := others[2].(string); ok && len(v) > 0 {
			cookie.Domain = v
		}
	}

	if len(others) > 3 {
		switch v := others[3].(type) {
		case bool:
			cookie.Secure = v
		default:
			if others[3] != nil {
				cookie.Secure = true
			}
		}
	}

	if len(others) > 4 {
		if v, ok := others[4].(bool); ok && v {
			cookie.HttpOnly = true
		}
	}

	if len(others) > 5 {
		if v, ok := others[5].(time.Time); ok {
			cookie.Expires = v
			cookie.RawExpires = v.Format(time.UnixDate)
		}
	}

	ctx.w.Header().Add("Set-Cookie", cookie.String())
}
開發者ID:elago,項目名稱:ela,代碼行數:57,代碼來源:ctx.go

示例12: SetCookie

// Duration is the cookie time-to-live in seconds (0 = forever).
func (ctx *Context) SetCookie(name, value string, age int64) {
	var utctime time.Time
	if age == 0 {
		// 2^31 - 1 seconds (roughly 2038)
		utctime = time.Unix(2147483647, 0)
	} else {
		utctime = time.Unix(time.Now().Unix()+age, 0)
	}
	cookie := http.Cookie{Name: name, Value: value, Expires: utctime, Path: "/"}
	ctx.Header().Add("Set-Cookie", cookie.String())
}
開發者ID:xyproto,項目名稱:web,代碼行數:12,代碼來源:cookie.go

示例13: commitSession

func commitSession(headers Headers, env Env, key, secret string, newValue string, options *CookieOptions) {
	cookie := new(http.Cookie)
	cookie.Name = key
	cookie.Value = newValue
	cookie.Path = options.Path
	cookie.Domain = options.Domain
	cookie.MaxAge = options.MaxAge
	cookie.Secure = options.Secure
	cookie.HttpOnly = options.HttpOnly
	headers.Add("Set-Cookie", cookie.String())
}
開發者ID:dangchienhsgs,項目名稱:mango,代碼行數:11,代碼來源:sessions.go

示例14: commitSession

func commitSession(headers Headers, env Env, key, secret string, options *CookieOptions) {
	cookie := new(http.Cookie)
	cookie.Name = key
	cookie.Value = encodeCookie(env["mango.session"].(map[string]interface{}), secret)
	cookie.Path = options.Path
	cookie.Domain = options.Domain
	cookie.MaxAge = options.MaxAge
	cookie.Secure = options.Secure
	cookie.HttpOnly = options.HttpOnly
	headers.Add("Set-Cookie", cookie.String())
}
開發者ID:kobeld,項目名稱:mango,代碼行數:11,代碼來源:sessions.go

示例15: WriteCookie

func (s *Session) WriteCookie(options cookieOptions, response http.ResponseWriter) {
	cookie := http.Cookie{
		Name:     options.Name,
		Value:    s.ID,
		Expires:  time.Now().Add(options.MaxAge),
		HttpOnly: options.HttpOnly,
		Secure:   options.Secure,
	}

	response.Header().Set("Set-Cookie", cookie.String())
}
開發者ID:xrstf,項目名稱:raziel,代碼行數:11,代碼來源:session.go


注:本文中的net/http.Cookie.String方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。