Cookie not decoded?

Hi,

I am trying to save a cookie (which work) and get the values back.

The code used are from (there are several similar links out there basically the same code):

func getuser(r *http.Request) string {
	var usr string
	if cookie, err := r.Cookie("session"); err == nil {
		cookieValue := make(map[string]string)
		if err = cookieHandler.Decode("session", cookie.Value, &cookieValue); err == nil {
			usr = cookieValue["name"]
		}

		fmt.Println("---cookie.Value---")
		fmt.Println(cookie.Value)
		fmt.Println("---cookie.Value---")

		fmt.Println("---cookieValue---")
		fmt.Println(cookieValue)
		fmt.Println("---cookieValue---")

		fmt.Println("---usr---")
		fmt.Println(usr)
		fmt.Println("---usr---")
	}

	return usr
}

The result of this code is that I can fetch the encrypted value, but not the decoded.

—cookie.Value— (seems to be OK)
TU5MjE0NTA2NXxkTnl3Mll6ZXZMeGhBeWVVYXRmMklFUHU4VFRXZWV3dGNyMktXMUt0ckE2SHAwM1M3MlJleUNDV2FGdWpFVW5SRUdmNjJ0N1MtTEV0cndPcW5BPT18AuIFYfSD96wKkbwWk7lTQLZgh8d1CHtsk1oHCNSI8vU=
—cookie.Value—

—cookieValue—(The map is empty)>
map[]
—cookieValue—

—usr— (no user name)
—usr—

What am I doing wrong? Or thinking wrong?

Check the returned error

securecookie: the value is not valid

How do I make it valid? Is it the encoding that fails?

EDIT: When I set the cookie, I get this “encoded”

session=MTU5MjE0OTUyNHx5QUFXdkxNbzQzbDBSWFl3SDRWclZlbjVTN3hYMlp1c3c3bFloaGNpZEpPR0pwNjQxVTRZcWtFUkd4c01Uc1RjdGY5OFpaMW90Q3JLTy04LUFBPT189LXZAtsR7Ghw_CvOsY6ZSAJ0dhD_hjtxqBrQNRS3Klw=; Path=/ <-------set cookie

When I get the cookie, I get the same:

—cookie.Value—

MTU5MjE0ODk5NHwzbmcxSkVyXzlzWFJ4Ukp5bWw4aC1jRGkxdVcwM2huNWdUYjNvb2swYVBQalZ0UWhvVzREdGJRWTBkUU5QZFc5UFItWUZERFVKVm9uMXJzYk1nPT188FTxxtXfFpepxtOPUeE4ifdNdqyMzRHPYp2avn0P7yY=

Why do I get this error?

securecookie: the value is not valid

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.