Just set the forms authentication method to Encryption and validation .
This will protect against bitflip attacks, since the signature will not match if the bit is turned upside down. Verification means that the message is signed.
How does validation work?
The verification algorithm can be set in web.config in the validationAlgorithm attribute of the machineKey element .
The default is HMACSHA256, which is a SHA-256 hash using HMAC .
The hash indicates a cookie - if the cookie value changes, the hash will no longer match. Since the end user does not know the secret, they cannot change the value of the cookie.
eg. Try here to generate a SHA-256 HMAC for the foo message using a secret secret .
This should give you: 773ba44693c7553d6ee20f61ea5d2757a9a4f4a44d2841ae4e95b52e4cd62db4
Note that if you change foo to something else, the hash will be different. This way you can protect cookies from unauthorized access. This cookie will be set as follows.
Set-Cookie: id=foo&hash=773ba44693c7553d6ee20f61ea5d2757a9a4f4a44d2841ae4e95b52e4cd62db4
Note that the value is explicit, but the hash signature prevents any interference. This is just a test.
With the encryption and verification option, foo will be encrypted first, and the signature will prevent any bit switching. This allows you to store values in a closed form from the end user.
If you implement this outside of forms authentication, remember, however, to save a certain type of expiration date in a cookie and include this in your signature. This date can be checked on the server side. Default Authentication does this by default.
Otherwise, the user may mark a valid cookie value. Say they are granted administrator access for one day and the following cookie is issued:
username=admin&hash=71b3ba92493e92ce3c60042988e9de428f44b35a6be61c8da99fa43f950d3056
The next day, when the administrator’s access is canceled, all the user needs to do is use the cookie editor to set their cookies to the above value, and they will gain administrator access to the system again.
To fix this, you will select the following cookie:
username=admin&expiry=20150508100000&hash=e38a3a003b30ceb9060165d19bb8d2a2bca6c7c531a37e888448ca417166db3a
It has an expiration date in the cookie that is signed in the hash. Any attempt to change the expiration date will result in an HMAC mismatch and the user will not have access. This will prevent any interference with the cookie expiration date or by any client resting party.
What are our other don't-mess-with-my-cookie options if you don't use forms-based authentication?
Another method is to save a completely random cryptographically secure generated string and set it as a cookie value. On your server, save the hash using SHA-2 (no salt is needed) and find this value to get information about the user's session. Of course, this has a few more overheads. The advantage is that sessions can be destroyed on the server side by deleting the record.
This can also be done using forms authentication, however you will need to implement a custom provider.