Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpGet("[action]")]
- public ActionResult<Receipt> CookieData(string? data)
- {
- // Append (or recreate) cookie
- HttpContext.Response.Cookies.Append(
- "timestamp",
- DateTime.Now.ToUniversalTime().ToString(),
- new CookieOptions
- {
- Expires = DateTimeOffset.Now.AddDays(1), // Make it a permanent cookie
- Domain = Request.Host.Host,
- Path = "/",
- });
- // If a value is passed to action and bound, append (or recreate) cookie
- // Otherwise read cookie from request
- if (!string.IsNullOrEmpty(data))
- {
- HttpContext.Response.Cookies.Append(
- "data",
- data,
- new CookieOptions
- {
- Expires = null, // Make it a session cookie
- });
- return Ok($"Data cookie set to {data}.");
- }
- else
- {
- var storedData = HttpContext.Request.Cookies["data"];
- return Ok($"Current data cookie is {storedData}.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement