I am creating a cookie to save the culture and thus be able to make an internationalized website. I have a controller HomeController that has an inheritance with another called BaseController that sets the language according to the text retrieved from the cookie, but at the time of executing the sequence, it goes through the method that generates the cookie but in BaseController, when retrieving it, it returns null , in the DevOps debugging it shows me that there is no cookie.
Problem derived from How do I update the culture using CultureHelper.GetImplementedCulture
BaseController
public class BaseController : Controller
{
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
string cultureName = null;
// Obtengo la cookie
HttpCookie cultureCookie = Request.Cookies["_culture"];
if (cultureCookie != null)
cultureName = cultureCookie.Value;
else
cultureName = Request.UserLanguages != null && Request.UserLanguages.Length > 0 ? Request.UserLanguages[0] : null; // obtengo los lenguajes aceptados
// Valido el nombre de la cultura
cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe
// Modifico el hilo con la nueva cultura
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
return base.BeginExecuteCore(callback, state);
}
}
HomeController
[Authorize]
public class HomeController : BaseController //Herencia de BaseController
{
// GET: /Home/Index/
public ActionResult Index()
{
return View();
}
[AllowAnonymous]
[TsVisible]
public ActionResult SetCulture(string culture)
{
// Valido la cultura
culture = CultureHelper.GetImplementedCulture(culture);
// Obtengo la cookie
HttpCookie cookie = Request.Cookies["_culture"];
if (cookie != null)
{
cookie.Value = culture; // Si no es nula la sobreescribo
cookie.Secure = true;
}
else
{
//Si es nula genero una nueva
cookie = new HttpCookie("_culture");
cookie.Value = culture;
cookie.Expires = DateTime.Now.AddYears(1);
cookie.Secure = true;
}
Response.Cookies.Add(cookie);
return RedirectToAction("Login","Account");
}
}
In theory with this it would generate the cookie but it does not. I have made different stop points in the code to monitor if it executes the sequence correctly, it executes correctly but does not generate the cookie, I leave some captures in the stops:
And in console:
Why is this happening?
The
Secure
cookie property limits its use to secure HTTPS connections, so it left two options:less recommended
Delete the Secure property
recommendable
In the IIS Express configuration:
SSL Enabled
to True.SSL Url
tohttps://localhost:[PUERTO]/