You can use HttpOnly, which prevents the client-side script from accessing the cookie via the document.cookie property. Cookies will still be circular, but will not be accessible by script and will not be stolen.
With ASP.NET 1.1, add the code below to Global.asax:
protected void Application_EndRequest(Object sender, EventArgs e) { foreach(string cookie in Response.Cookies) { const string HTTPONLY = ";HttpOnly"; string path = Response.Cookies[cookie].Path; if (path.EndsWith(HTTPONLY) == false) {
With ASP.NET 2.0 and higher, you can use web.config:
<system.web> <httpCookies httpOnlyCookies="true" /> </system.web>
If security is something very important on your system, the best approach is to invest in an SSL (Secure Sockets Layer) connection to your site. You can then set the cookie property, which causes the cookie to be sent only if an SSL connection is present. SSL does not protect the cookie from reading or manipulation while it is on the user's computer, but it prevents the cookie from being read during transport because the cookie is encrypted. This approach requires an SSL certificate.
This can be done in the httpcookies web.config element.
<system.web> <httpCookies requireSSL="true" /> </system.web>
If the connection is not SSL, the cookie is not sent to the server.
More details:
ASP.NET Cookies FAQ
Develop and deploy secure web applications using ASP.NET 2.0 and IIS 6.0
source share