Answered by:
Cookies are not saved in my MVC 5 project

Question
-
User206383436 posted
I am about to get crazy because I cannot save cookies. I have been dealing with this issue for 10 days and I have also watched a lot of support posts but none of them has the key to solve my issue. I am writing an ASP.NET MVC 5 application with VS2017. The code to read and save cookies is as follows. The Utilerias.NuevoEventoSistema method inserts a line in a text/log file and I always get "The cookie does not exist".
public ActionResult RegistraVoto(String id) { ObtenerCookies(); return View(); } private String ObtenerCookies() { String strUserId = ""; if (Request.Cookies["ccIdusuario"] != null) { Utilerias.NuevoEventoSistema("1. Cookie exists.", "", "", "", ""); HttpCookie UserIdCookie = Request.Cookies["ccIdusuario"]; strUserId = UserIdCookie.Value.ToString(); Utilerias.NuevoEventoSistema("3. Valor de la cookie", strUserId, "", "", ""); } else { strUserId = Guid.NewGuid().ToString(); Utilerias.NuevoEventoSistema("1. Cookie does not exist.", "", "", "", ""); HttpCookie UserCookie = new HttpCookie("ccIdusuario"); UserCookie.Value = strUserId; UserCookie.Expires = DateTime.Now.AddYears(1); Response.Cookies.Add(UserCookie); Utilerias.NuevoEventoSistema("4. Valor de cookie", strUserId, "", "", ""); } return strUserId; }
The RegistraVoto controller is called from a View as follos:
<script type="text/javascript">
function RegistraVoto(id, puntos) { var parametro = String(puntos) + '-' + String(id); var strURL = encodeURI("/ListasPopularidad/RegistraVoto/" + parametro); // Issue POST to server. $.post(strURL, { }, function (data) { alert(data); }); }
</script>I have tested in Chrome and FireFox with the same result. I have made sure cookies are enabled too. I am running the project directly in VS2017 in localhost.
I will very much appreciate any help that drives me to fix my issue.
P.D.: I implementd this same ObtenerCookies() method in a web forms website without any problem. I wonder if there is a special consideration for MVC. Or maybe the JavaScript code above has something to do.
With respect,
Jorge MaldonadoMonday, July 2, 2018 10:22 PM
Answers
-
User1724605321 posted
Hi JORGEMAL ,
You are using ajax to server side function to set the session value , so just use the JsonResult :
public JsonResult RegistraVoto(String id) { var strID=ObtenerCookies(); return Json(strID, JsonRequestBehavior.AllowGet); }
Return view in your code will initialize the page and not return the response .
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 3, 2018 5:42 AM
All replies
-
User1724605321 posted
Hi JORGEMAL ,
You are using ajax to server side function to set the session value , so just use the JsonResult :
public JsonResult RegistraVoto(String id) { var strID=ObtenerCookies(); return Json(strID, JsonRequestBehavior.AllowGet); }
Return view in your code will initialize the page and not return the response .
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 3, 2018 5:42 AM -
User206383436 posted
Thank you very, very, very much. I really appreciate your valuable feedback. Your answer solved my issue.
Respectfully,
Jorge MaldonadoTuesday, July 3, 2018 2:09 PM