0

Hi Trying to create a simple username cookie. seems to add but when I refresh the page the cookie has gone, any ideas?

 [HttpGet]
            public ActionResult Login()
            {
                var model = new ViewModels.UserCredentials();
                ViewBag.Title = "Login";
                model.UserName = CheckLoginCookie();
                model.RememberMe = !string.IsNullOrEmpty(model.UserName);
                return View("Login",model);
            }
     private string CheckLoginCookie()
            {
                    if (Response.Cookies.Get("username") != null)
                    {
                        return Response.Cookies["username"].Value;
                    }

                return string.Empty;
            }

     [HttpPost]
    public ActionResult Login(ViewModels.UserCredentials credentials)
    {
    //do lots of stuff
    //Create username remember cookie
                if (credentials.RememberMe)
                {
                    HttpCookie ckUserName = new HttpCookie("username");
                    ckUserName.Expires = DateTime.Now.AddSeconds(500)
//yeap i know its only 500;
                    ckUserName.Value = credentials.UserName;
                    Response.Cookies.Add(ckUserName);
                }

    }
D-W
  • 5,201
  • 14
  • 47
  • 74

1 Answers1

3

To check the cookies cming from the client you need to look in to Request not the Response.

private string CheckLoginCookie()
{
    if (Request.Cookies.Get("username") != null)
    {
        return Request.Cookies["username"].Value;
    }
    return string.Empty;
}
Chetan
  • 6,711
  • 3
  • 22
  • 32