<p>Say a user is browsing a website, and then performs some action which changes the database (let's say they add a comment). When the request to actually add the comment comes in, however, we find we need to force them to login before they can continue.</p>

<p>Assume the login page asks for a username and password, and redirects the user back to the URL they were going to when the login was required. That redirect works find for a URL with only GET parameters, but if the request originally contained some HTTP POST data, that is now lost.</p>

<p>Can anyone recommend a way to handle this scenario when HTTP POST data is involved?</p>

<p>Obviously, if necessary, the login page could dynamically generate a form with all the POST parameters to pass them along (though that seems messy), but even then, I don't know of any way for the login page to redirect the user on to their intended page while keeping the POST data in the request.</p>

<hr>

<p><strong>Edit</strong> : One extra constraint I should have made clear - Imagine we don't know if a login will be required until the user submits their comment. For example, their cookie might have expired between when they loaded the form and actually submitted the comment.</p>

[{'answer_id': 53267, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 4, 'selected': False, 'text': '<p>This is one good place where Ajax techniques might be helpful. When the user clicks the submit button, show the login dialog on client side and validate with the server before you actually submit the page.</p>\n\n<p>Another way I can think of is showing or hiding the login controls in a DIV tag dynamically in the main page itself.</p>\n'}, {'answer_id': 53270, 'author': 'castaway', 'author_id': 4840, 'author_profile': 'https://Stackoverflow.com/users/4840', 'pm_score': 1, 'selected': False, 'text': "<p>Collect the data on the page they submitted it, and store it in your backend (database?) while they go off through the login sequence, hide a transaction id or similar on the page with the login form. When they're done, return them to the page they asked for by looking it up using the transaction id on the backend, and dump all the data they posted into the form for previewing again, or just run whatever code that page would run.</p>\n\n<p>Note that many systems, eg blogs, get around this by having login fields in the same form as the one for posting comments, if the user needs to be logged in to comment and isn't yet.</p>\n"}, {'answer_id': 53289, 'author': 'Robert Swisher', 'author_id': 1852, 'author_profile': 'https://Stackoverflow.com/users/1852', 'pm_score': 2, 'selected': False, 'text': '<p>Just store all the necessary data from the POST in the session until after the login process is completed. Or have some sort of temp table in the db to store in and then retrieve it. Obviously this is pseudo-code but:</p>\n\n<pre><code>if ( !loggedIn ) {\n    StorePostInSession();\n    ShowLoginForm();\n}\n\nif ( postIsStored ) {\n    RetrievePostFromSession();\n}\n</code></pre>\n\n<p>Or something along those lines.</p>\n'}, {'answer_id': 53315, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 3, 'selected': True, 'text': '<p>2 choices:</p>\n\n<ol>\n<li>Write out the messy form from the login page, and JavaScript form.submit() it to the page.</li>\n<li>Have the login page itself POST to the requesting page (with the previous values), and have that page\'s controller perform the login verification. Roll this into whatever logic you already have for detecting the not logged in user (frameworks vary on how they do this). In pseudo-MVC:</li>\n</ol>\n\n<pre><code>\n        CommentController {\n           void AddComment() {\n             if (!Request.User.IsAuthenticated && !AuthenticateUser()) {\n                return;\n             }\n             // add comment to database\n           }\n\n           bool AuthenticateUser() {\n             if (Request.Form["username"] == "") {\n                // show login page\n                foreach (Key key in Request.Form) {\n                   // copy form values\n                   ViewData.Form.Add("hidden", key, Request.Form[key]);\n                }\n                ViewData.Form.Action = Request.Url;\n\n                ShowLoginView();\n                return false;\n              } else {\n                 // validate login\n                 return TryLogin(Request.Form["username"], Request.Form["password"]);\n              } \n           }\n        }\n</code></pre>\n'}, {'answer_id': 53330, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>I know it says language-agnostic, but why not take advantage of the conventions provided by the server-side language you are using? If it were Java, the data could persist by setting a Request attribute. You would use a controller to process the form, detect the login, and then forward through. If the attributes are set, then just prepopulate the form with that data?</p>\n\n<p><strong>Edit:</strong> You could also use a Session as pointed out, but I'm pretty sure if you use a forward in Java back to the login page, that the Request attribute will persist.</p>\n"}, {'answer_id': 53342, 'author': 'Jacob Rigby', 'author_id': 5357, 'author_profile': 'https://Stackoverflow.com/users/5357', 'pm_score': 2, 'selected': False, 'text': '<p>You might want to investigate why Django <a href="http://www.djangoproject.com/weblog/2008/sep/02/security/" rel="nofollow noreferrer">removed this feature</a>  before implementing it yourself.  It doesn\'t seem like a Django specific problem, but rather yet another cross site forgery attack.</p>\n'}]
['https://Stackoverflow.com/questions/53260', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/797/']