I am setting and getting cookies in a ASP.NET Core WebApp. The Microsoft Docs says "After you add a cookie by using the HttpResponse.Cookies collection, the cookie is immediately available in the HttpRequest.Cookies collection, even if the response has not been sent to the client". However, I find that a cookie can't be read immediately after setting; see Set() below which always returns false. That said, the browser's storage inspector shows that it was actually set.
SO 3230133 raised the same issue in the context of PHP and the answer suggested that the cookie isn't set until the response is sent back to the client, and isn't available in your PHP until the next request from the client after that. Is this the same true in the context of ASP.NET Core? Is there a workaround similar to PHP $_COOKIE['uname'] for setting the cookie value somewhere so it can be read before the next request from the client?
The Microsoft Docs relate to .NetFramework 4.8, but there isn't an alternative for .Net Core. Is this something that works for the former, but not the latter?
public class MyCookies
{
public IHttpContextAccessor Accessor { get; private set; }
public MyCookies(IHttpContextAccessor accessor)
{
Accessor = accessor;
}
public bool Set(string name, string value)
{
var rc = false;
Accessor.HttpContext.Response.Cookies.Append(name, value);
if (Accessor.HttpContext.Request.Cookies.TryGetValue(name, out var result) && (result == value))
rc = true;
return rc;
}
}
public class SettingsModel : PageModel
{
private readonly IHttpContextAccessor _httpContextAccessor;
public SettingsModel(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public IActionResult OnPost()
{
var cookies = new MxCookies(_httpContextAccessor);
if (cookies.SetValue(".AspNetCore.Culture", "fr-CH") == false)
{
var stop = true;
}
return new RedirectToPageResult("Index");
}
}
Don't forget to invoke services.AddHttpContextAccessor() in Startup.ConfigureServices()