<p>This is related to <a href="https://stackoverflow.com/questions/43324/can-i-put-an-aspnet-session-id-in-a-hidden-form-field">another question I asked</a>. In summary, I have a special case of a URL where, when a form is POSTed to it, I can't rely on cookies for authentication or to maintain the user's session, but I somehow need to know who they are, and I need to know they're logged in!</p>

<p>I think I came up with a solution to my problem, but it needs fleshing out. Here's what I'm thinking. I create a hidden form field called "username", and place within it the user's username, encrypted. Then, when the form POSTs, even though I don't receive any cookies from the browser, I know they're logged in because I can decrypt the hidden form  field and get the username.</p>

<p>The major security flaw I can see is replay attacks. How do I prevent someone from getting ahold of that encrypted string, and POSTing as that user? I know I can use SSL to make it harder to steal that string, and maybe I can rotate the encryption key on a regular basis to limit the amount of time that the string is good for, but I'd really like to find a bulletproof solution. Anybody have any ideas? Does the ASP.Net ViewState prevent replay? If so, how do they do it?</p>

<p><strong>Edit</strong>: I'm hoping for a solution that doesn't require anything stored in a database. Application state would be okay, except that it won't survive an IIS restart or work at all in a web farm or garden scenario. I'm accepting Chris's answer, for now, because I'm not convinced it's even possible to secure this without a database. But if someone comes up with an answer that does not involve the database, I'll accept it!</p>

[{'answer_id': 44400, 'author': 'Chris AtLee', 'author_id': 4558, 'author_profile': 'https://Stackoverflow.com/users/4558', 'pm_score': 3, 'selected': False, 'text': "<p>You could use some kind of random challenge string that's used along with the username to create the hash.  If you store the challenge string on the server in a database you can then ensure that it's only used once, and only for one particular user.</p>\n"}, {'answer_id': 44410, 'author': 'Matt Bishop', 'author_id': 4301, 'author_profile': 'https://Stackoverflow.com/users/4301', 'pm_score': 0, 'selected': False, 'text': '<p>If you only accept each key once (say, make the key a GUID, and then check when it comes back), that would prevent replays. Of course, if the attacker responds <em>first</em>, then you have a new problem...</p>\n'}, {'answer_id': 44416, 'author': 'Ryan Lanciaux', 'author_id': 1385358, 'author_profile': 'https://Stackoverflow.com/users/1385358', 'pm_score': 0, 'selected': False, 'text': '<p>Is this WebForms or MVC? If it\'s MVC you could utilize the AntiForgery token. This seems like it\'s similar to the approach you mention except it uses basically a GUID and sets a cookie with the guid value for that post. For more on that see Steve Sanderson\'s blog: <a href="http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/" rel="nofollow noreferrer">http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/</a></p>\n\n<p>Another thing, have you considered checking the referrer on the postback? This is not bulletproof but it may help.</p>\n'}, {'answer_id': 44422, 'author': 'matt b', 'author_id': 4249, 'author_profile': 'https://Stackoverflow.com/users/4249', 'pm_score': 1, 'selected': False, 'text': '<p>Can you use memory or a database to maintain <em>any</em> information about the user or request at all?</p>\n\n<p>If so, then on request for the form, I would include a hidden form field whose contents are a randomly generated number. Save this token to in application context or some sort of store (a database, flat file, etc.) when the request is rendered. When the form is submitted, check the application context or database to see if that randomly generated number is still valid (however you define valid - maybe it can expire after X minutes). If so, remove this token from the list of "allowed tokens". </p>\n\n<p>Thus any replayed requests would include this same token which is no longer considered valid on the server.</p>\n'}, {'answer_id': 44565, 'author': 'Ethan Post', 'author_id': 4527, 'author_profile': 'https://Stackoverflow.com/users/4527', 'pm_score': 1, 'selected': False, 'text': '<p>I am new to some aspects of web programming but I was reading up on this the other day. I believe you need to use a <a href="http://www.intertwingly.net/blog/1585.html" rel="nofollow noreferrer">Nonce</a>.</p>\n'}, {'answer_id': 65931, 'author': 'Chris Kite', 'author_id': 9573, 'author_profile': 'https://Stackoverflow.com/users/9573', 'pm_score': 4, 'selected': False, 'text': '<p>If you really don\'t want to store any state, I think the best you can do is limit replay attacks by using timestamps and a short expiration time. For example, server sends:</p>\n\n<p>{Ts, U, HMAC({Ts, U}, Ks)}</p>\n\n<p>Where Ts is the timestamp, U is the username, and Ks is the server\'s secret key. The user sends this back to the server, and the server validates it by recomputing the HMAC on the supplied values. If it\'s valid, you know when it was issued, and can choose to ignore it if it\'s older than, say, 5 minutes.</p>\n\n<p>A good resource for this type of development is <a href="http://pdos.csail.mit.edu/papers/webauth:sec10.pdf" rel="noreferrer">The Do\'s and Don\'ts of Client Authentication on the Web</a></p>\n'}, {'answer_id': 65953, 'author': 'Yes - that Jake.', 'author_id': 5287, 'author_profile': 'https://Stackoverflow.com/users/5287', 'pm_score': 5, 'selected': True, 'text': "<p>If you hash in a time-stamp along with the user name and password, you can close the window for replay attacks to within a couple of seconds. I don't know if this meets your needs, but it is at least a partial solution.</p>\n"}, {'answer_id': 652472, 'author': 'Kelsey', 'author_id': 8707, 'author_profile': 'https://Stackoverflow.com/users/8707', 'pm_score': 2, 'selected': False, 'text': "<p>In one of my apps to stop 'replay' attacks I have inserted IP information into my session object.  Everytime I access the session object in code I make sure to pass the Request.UserHostAddress with it and then I compare to make sure the IPs match up.  If they don't, then obviously someone other than the person made this request, so I return null.  It's not the best solution but it is at least one more barrier to stop replay attacks.</p>\n"}, {'answer_id': 652516, 'author': 'rama-jka toti', 'author_id': 75248, 'author_profile': 'https://Stackoverflow.com/users/75248', 'pm_score': 1, 'selected': False, 'text': "<p>(Replay attacks can easily be all about an IP/MAC spoofing, plus you're challenged on dynamic IPs )</p>\n\n<p>It is not just replay you are after here, in isolation it is meaningless. Just use SSL and avoid handcrafting anything..</p>\n\n<p>ASP.Net ViewState is a mess, avoid it. While PKI is heavyweight and bloated, at least it works without inventing your own security 'schemes'. So if I could, I'd use it and always go for mutual authent. Server-only authentification is quite useless.</p>\n"}, {'answer_id': 2092673, 'author': 'awe', 'author_id': 109392, 'author_profile': 'https://Stackoverflow.com/users/109392', 'pm_score': 1, 'selected': False, 'text': '<p>The ViewState includes security functionality. See <a href="http://msdn.microsoft.com/en-us/library/ms972969.aspx#securitybarriers_topic5" rel="nofollow noreferrer">this article</a> about some of the build-in security features in ASP.NET . It does validation against the server machineKey in the machine.config on the server, which ensures that each postback is valid.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms972969.aspx#securitybarriers_topic8" rel="nofollow noreferrer">Further down in the article</a>, you also see that if you want to store values in your own hidden fields, you can use the <code>LosFormatter</code> class to encode the value in the same way that the ViewState uses for encryption.</p>\n\n<pre><code>private string EncodeText(string text) {\n  StringWriter writer = new StringWriter();\n  LosFormatter formatter = new LosFormatter();\n  formatter.Serialize(writer, text);\n  return writer.ToString();\n}\n</code></pre>\n'}, {'answer_id': 12472337, 'author': 'Darrell Teague', 'author_id': 1679516, 'author_profile': 'https://Stackoverflow.com/users/1679516', 'pm_score': 4, 'selected': False, 'text': '<p>There are several good answers here and putting them all together is where the answer ultimately lies:</p>\n\n<ol>\n<li><p>Block-cipher encrypt (with AES-256+) and hash (with SHA-2+) all state/nonce related information that is sent to a client. Hackers with otherwise just manipulate the data, view it to learn the patterns and circumvent everything else. Remember ... it only takes one open window.</p></li>\n<li><p>Generate a one-time random and unique nonce per request that is sent back with the POST request. This does two things: It ensures that the POST response goes with THAT request. It also allows tracking of one-time use of a given set of get/POST pairs (preventing replay).</p></li>\n<li><p>Use timestamps to make the nonce pool manageable. Store the time-stamp in an encrypted cookie per #1 above. Throw out any requests older than the maximum response time or session for the application (e.g., an hour).</p></li>\n<li><p>Store a "reasonably unique" digital fingerprint of the machine making the request with the encrypted time-stamp data. This will prevent another trick wherein the attacker steals the clients cookies to perform session-hijacking. This will ensure that the request is coming back not only once but from the machine (or close enough proximity to make it virtually impossible for the attacker to copy) the form was sent to.</p></li>\n</ol>\n\n<p>There are ASPNET and Java/J2EE security filter based applications that do all of the above with zero coding. Managing the nonce pool for large systems (like a stock trading company, bank or high volume secure site) is not a trivial undertaking if performance is critical. Would recommend looking at those products versus trying to program this for each web-application.</p>\n'}, {'answer_id': 42481101, 'author': 'Clintm', 'author_id': 3384609, 'author_profile': 'https://Stackoverflow.com/users/3384609', 'pm_score': 1, 'selected': False, 'text': '<p>Use https... it has replay protection built in.</p>\n'}]
['https://Stackoverflow.com/questions/44391', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2527/']