I’m trying to protect my .NET web site against CSRF attacks using a hidden key in every form and an special temp cookie, so when the user POST the form I can compare the temp cookie key and the hidden key in the form.
But I don’t wanna use Session or other shared object to keep those temp keys, so I’ve come up with this way:
- Browser ask for a form (GET).
- App generates a key, [userId] +
[currentDateTime], symmetrically
encrypted with a key that my app
knows. - App put that key in a hidden field
in the form, and sent a cookie with
that key too. Browser POST the form. -
App ensures that:
- The cookie value and hidden form value are the same.
- Can obtain an [userId] from the decrypted value, and it’s the current user id.
- Can obtain a [DateTime] from the decrypted value.
- [DateTime] obtained is not more than 15 min old.
-
Otherwise, reject POST and show error.
Do you see any flaw?
Kind regards.
The strategy you describe works in general, and is known as “Double Submitting Cookies“. BUT there are a few things that you should know about