Cookies
A cookie is a small piece of data the server asks the browser to store and send back with every later request. This lesson shows how to write, read and delete cookies in ASP.NET Core and which options keep them safe.
- StartServer sets itResponse.Cookies.Append adds a Set-Cookie header
- Step 2Browser stores itKept until it expires, or until the browser closes
- Step 3Browser sends it backEvery request to your site includes it in the Cookie header
- Step 4Server reads itRequest.Cookies["theme"] returns the value
- EndIt endsIt expires or the server deletes it
The idea in plain words
Cookies are the browser's small notebook for your site. They are perfect for small, non-sensitive preferences such as a theme or language, and ASP.NET Core itself uses them for sign-in, session ids, TempData and anti-forgery tokens.
Because cookies live on the user's computer, users can read, change or delete them. Never store passwords, prices or roles in a plain cookie. Keep them small too: browsers limit each cookie to about 4 KB, and every cookie is sent with every request.
Example: write, read and delete
public class SettingsController : Controller
{
[HttpPost, ValidateAntiForgeryToken]
public IActionResult SetTheme(string theme)
{
Response.Cookies.Append("theme", theme, new CookieOptions
{
HttpOnly = true, // not readable by JavaScript
Secure = true, // only sent over HTTPS
SameSite = SameSiteMode.Lax, // limits cross-site sending
Expires = DateTimeOffset.UtcNow.AddDays(30)
});
return RedirectToAction("Index", "Home");
}
public IActionResult Index()
{
var theme = Request.Cookies["theme"] ?? "light";
return View(model: theme);
}
[HttpPost, ValidateAntiForgeryToken]
public IActionResult ResetTheme()
{
Response.Cookies.Delete("theme");
return RedirectToAction(nameof(Index));
}
}Cookie options that matter
- HttpOnly: JavaScript cannot read the cookie, which limits damage from XSS.
- Secure: only sent over HTTPS.
- SameSite: Lax or Strict reduces cross-site request forgery risks.
- Expires or MaxAge: without one, the cookie is deleted when the browser closes.
- IsEssential: marks a cookie as needed for the app to work when a cookie consent policy is in place.
Common mistakes
- Storing sensitive or trusted data in a cookie without protection.
- Forgetting HttpOnly and Secure on cookies that matter.
- Expecting a cookie set in this response to appear in Request.Cookies in the same request; it arrives with the next request.
Interview question
What do the HttpOnly, Secure and SameSite cookie options do?
HttpOnly stops client-side JavaScript from reading the cookie, reducing the impact of cross-site scripting. Secure makes the browser send the cookie only over HTTPS. SameSite controls whether the cookie is sent with cross-site requests: Strict never, Lax only for top-level navigations such as following a link, and None always (which requires Secure). Together they protect authentication and session cookies.
Want the full details? Read the official Microsoft documentation for this topic.