I'm trying to set and get cookies in a project I'm working on in asp.net mvc4.
This is how I'm setting the cookies:
var Username = new HttpCookie("Username");
Username.Value = model.UserName;
Username.Expires = DateTime.Now.AddDays(7);
Username.Secure = true;
Response.Cookies.Add(Username);
Then, in other controller action, I'm trying this:
HttpCookie cookie = Request.Cookies["Username"];
but I'm getting null for that specific cookie. Also, I don't know if that would make a difference but I'm not requesting the cookie in the action in which this current action redirects but in other action. That shouldn't make difference since I'm setting the expiration date +7 days from the creation date.
Laziale,
Your code is perfectly Ok.
This is only due to fact that your cookie is secure.
Username.Secure = true;
It does not sent back due to that reason only. Try remove that line. It shown perfectly in Request.Cookie collection
Hope helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With