How do I implement cookies in a web app

To implement cookies in a C# web application, you can use the HttpCookie class to create and manage cookies in the user's browser. Cookies are often used to store user preferences, authentication tokens, and other session data.

// Example of setting a cookie in C# HttpCookie myCookie = new HttpCookie("UserPreferences"); myCookie.Value = "DarkMode"; myCookie.Expires = DateTime.Now.AddDays(7); // Cookie will expire in 7 days Response.Cookies.Add(myCookie); // Example of reading a cookie in C# if (Request.Cookies["UserPreferences"] != null) { string userPreference = Request.Cookies["UserPreferences"].Value; // Use the user preference for application logic }

Cookies C# web application HttpCookie User Preferences session management web development