ASP.NET Core MVC Interview Question #123

What are cookies, and how are they used in MVC applications?

Authentication, Authorization, Security & State Management Mid-Level Intermediate

Quick Interview Answer

Cookies are small values stored by the browser and sent to the server according to their domain, path, expiration, SameSite, Secure, and other settings. They can support authentication, preferences, correlation, anti-forgery mechanisms, and other browser state.

Detailed Explanation

Cookies are small values stored by the browser and sent to the server according to their domain, path, expiration, SameSite, Secure, and other settings. They can support authentication, preferences, correlation, anti-forgery mechanisms, and other browser state.

Sensitive values should not simply be stored as readable cookie text. Authentication cookies use protected ticket formats, and application cookies should be configured according to their security purpose.

Code Example

Response.Cookies.Append(
    "theme",
    "dark",
    new CookieOptions
    {
        HttpOnly = true,
        Secure = true,
        SameSite = SameSiteMode.Lax
    });