views:

88

answers:

4

I am a lazy programmer, i dont want to write dozens or hundreds of lines if i dont need to.

Some actions such as posting a comment require login. It may timeout and the user may be redirected to a login page. With get data i can easily put the query in something like action=PrevPageUrlAndQuery. However with POST data i have to do something more complicated.

So is there a way i can make the page go backwards so all the user needs to do is click that post button again? Instead of displaying a page saying please hit back and resubmit your comment (more clicks + reading makes me slightly sad)

+1  A: 

When your POST handler for the comment button decides it needs a login, have it forward to the login page after setting up a request attribute that holds the original URL of the comment page. The login page should drop that URL into a hidden form parameter. WHen the login action completes successfully, it can just forward back to that original page's URL.

Pointy
wouldnt i lose the post data? wouldnt the comment box be empty?
acidzombie24
Well, you could stash that somewhere too, if you wanted. Basically what we're saying is that your server could "package up" the action, stick it someplace where it can find it again later, and then after login carry out the original POST.
Pointy
But then i could process the data right away and my question is about being lazy and not converting/holding that data.
acidzombie24
No, you most certainly can't process the data right away, because the user hasn't authenticated yet. (Unless by "right away" you mean "right after the user authenticates.) If you don't want to stash the form parameters, nothing's going to do the work for you.
Pointy
+1  A: 

When you redirect to the login page, send a "redirectUrl" query string parameter containing the URL you want to return to. On the login page, read this query string and react accordingly...

' Going to login page
Dim Redir As String = HttpUtility.UrlEncode("~/Summary.aspx?test=1")
Response.Redirect("~/Login.aspx?redirectUrl=" & Redir)

' On login page, after successful login
Dim Redir As String = "~/Default.aspx"
If Not String.IsNullOrEmpty(Request.QueryString("redirectUrl"))
    Redir = HttpUtility.UrlDecode(Request.QueryString("redirectUrl"))
End If
Response.Redirect(Redir)
Josh Stodola
StackOverflow syntax highlighting is **worthless**
Josh Stodola
You might want to check the redirectUrl against a regular expression to make sure it's not leaving your site. Otherwise you're enabling a pretty strong phishing attack.
Neall
@Neall when I do it in my site I only allow relative URLs, so attempts to redirect to http://phishers.net/hackme would just fail. Probably nice to check with a regex anyway.
Pointy
wouldnt i lose the post data? wouldnt the comment box be empty?
acidzombie24
Naturally, yes, because HTTP is stateless. You can persist that information with query strings as well, but it would probably be more suited for a session variable or cookie.
Josh Stodola
But then i could process the data right away and my question is about being lazy and not converting/holding that data.
acidzombie24
That's not how HTTP works. Session state *is* the lazy way out!
Josh Stodola
+1  A: 

What are you trying to do this in. A header redirect in the code would be the easiest way to go to the right page and preserve and post information. Otherwise you can use the history.back() in javascript but I think the redirector in the header is probably a safer bet.

Mech Software
acidzombie24
`history.back(val)` seems to only go back once no matter what value i give it :(
acidzombie24
did you try like -3 instead of positive integers?
Mech Software
A: 

<script>history.go(-2)</script>

acidzombie24