In C#, you can create a cookie that is modifiable from both the client-side (JavaScript) and server-side (C#) by ensuring the cookie meets these two criteria:
Key Requirements
HttpOnly = false — so JavaScript can read/modify it.
No Secure flag (unless using HTTPS).
Accessible Path — set Path = "/" if you want it globally accessible.
1. Set a Modifiable Cookie in C# (Server-Side)
In ASP.NET MVC or WebForms, you can use:
HttpCookie cookie = new HttpCookie("user-preference", "dark-mode");
cookie.Expires = DateTime.Now.AddDays(7); // Optional: persist cookie
cookie.HttpOnly = false; // Required to allow JavaScript access
cookie.Path = "/"; // Available site-wide
Response.Cookies.Add(cookie);
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In C#, you can create a cookie that is modifiable from both the client-side (JavaScript) and server-side (C#) by ensuring the cookie meets these two criteria:
Key Requirements
HttpOnly = false— so JavaScript can read/modify it.Secureflag (unless using HTTPS).Path = "/"if you want it globally accessible.1. Set a Modifiable Cookie in C# (Server-Side)
In ASP.NET MVC or WebForms, you can use:
In ASP.NET Core:
2. Access and Modify the Cookie via JavaScript (Client-Side)
3. Modify the Cookie in C# Again (Optional)
Important Notes