Dave
Dave

Reputation: 177

cookie check isn't working

I am putting a a "try it out" function on the front page of my site - basically a visitor can try a very limited set of what my site does, in browser, to see what it's all about.

I want to put a persistent cookie in to check if they've tried it out before, and if so direct them to the sign up page instead (I'm aware this isn't foolproof, but I'm not looking for Fort Knox, anyone looking to avoid that check will never sign up anyway, I'm just trying to nudge people who have liked the functionality to maybe sign up.)

When the user clicks the try it out button, my code is:

//check cookie to see if this has been tried before
    HttpCookie myCookie = HttpContext.Current.Request.Cookies["myCookie"];
    if (myCookie == null)
    {
generateLink()
}
else {
response.Redirect(~/SignUp);
}

and at the end of the generateLink() method I have the code:

HttpCookie myCookie = new HttpCookie("myCookie");
    myCookie.Values.Add("HasTried", "True");
    myCookie.Expires = DateTime.Now.AddHours(9680);

but the "try it out" button always generates a Link, and never fires the respone.redirect. Where am I going wrong?

Upvotes: 1

Views: 59

Answers (2)

NikolaiDante
NikolaiDante

Reputation: 18639

You need to add the cookie to the response:

Response.Cookies.Add(myCookie);

If you place that at the end of the code from generateLink() that'll write the cookie to browser.

See: How to write a cookie, from MSDN.

Upvotes: 3

Kathir
Kathir

Reputation: 11

Response.Cookies.Add(myCookie);

is missing at the end

Upvotes: 1

Related Questions