<p>ASP.NET server-side controls postback to their own page.  This makes cases where you want to redirect a user to an external page, but need to post to that page for some reason (for authentication, for instance) a pain.</p>

<p>An <code>HttpWebRequest</code> works great if you don't want to redirect, and JavaScript is fine in some cases, but can get tricky if you really do need the server-side code to get the data together for the post.</p>

<p>So how do you both post to an external URL and redirect the user to the result from your ASP.NET codebehind code?</p>

[{'answer_id': 5185, 'author': 'Mike Powell', 'author_id': 205, 'author_profile': 'https://Stackoverflow.com/users/205', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re using ASP.NET 2.0, you can do this with <a href="http://msdn.microsoft.com/en-us/library/ms178139.aspx" rel="nofollow noreferrer">cross-page posting</a>.</p>\n\n<p>Edit: I missed the fact that you\'re asking about an <em>external</em> page.  For that I think you\'d need to have your ASP.NET page gen up an HTML form whose action is set to the remote URL and method is set to POST.  (Using cross-page posting, this could even be a different page with no UI, only hidden form elements.)  Then add a bit of javascript to submit the form as soon as the postback result was received on the client.</p>\n'}, {'answer_id': 5190, 'author': 'palmsey', 'author_id': 521, 'author_profile': 'https://Stackoverflow.com/users/521', 'pm_score': 2, 'selected': False, 'text': "<p>I have done this by rendering a form that auto-posts (using JavaScript) to the desired remote URL - gather whatever information you need for the post in the web form's postback and then build the HTML for the remote-posting form and render it back to the client.</p>\n\n<p>I built a utility class for this that contains the remote URL and a collection of name/value pairs for the form.</p>\n\n<p>Cross-page posting will work if you own both of the pages involved, but not if you need to post to another site (PayPal, for example).</p>\n"}, {'answer_id': 5219, 'author': 'saalon', 'author_id': 111, 'author_profile': 'https://Stackoverflow.com/users/111', 'pm_score': 5, 'selected': True, 'text': '<p>Here\'s how I solved this problem today.  I started from <a href="http://www.c-sharpcorner.com/UploadFile/desaijm/ASP.NetPostURL11282005005516AM/ASP.NetPostURL.aspx" rel="nofollow noreferrer">this article</a> on C# Corner, but found the example - while technically sound - a little incomplete.  Everything he said was right, but I needed to hit a few external sites to piece this together to work exactly as I wanted.</p>\n\n<p>It didn\'t help that the user was not technically submitting a form at all; they were clicking a link to go to our support center, but to log them in an http post had to be made to the support center\'s site.</p>\n\n<p>This solution involves using <code>HttpContext.Current.Response.Write()</code> to write the data for the form, then using a bit of Javascript on the <code>&lt;body onload=""&gt;</code> method to submit the form to the proper URL.</p>\n\n<p>When the user clicks on the Support Center link, the following method is called to write the response and redirect the user:</p>\n\n<pre><code>public static void PassthroughAuthentication()\n{\n\n    System.Web.HttpContext.Current.Response.Write("&lt;body \n    onload=document.forms[0].submit();window.location=\\"Home.aspx\\";&gt;");\n\n    System.Web.HttpContext.Current.Response.Write("&lt;form name=\\"Form\\" \n    target=_blank method=post \n    action=\\"https://external-url.com/security.asp\\"&gt;");\n\n    System.Web.HttpContext.Current.Response.Write(string.Format("&lt;input \n       type=hidden name=\\"cFName\\" value=\\"{0}\\"&gt;", "Username"));\n\n    System.Web.HttpContext.Current.Response.Write("&lt;/form&gt;");\n    System.Web.HttpContext.Current.Response.Write("&lt;/body&gt;");\n}\n</code></pre>\n\n<p>The key to this method is in that onload bit of Javascript, which , when the body of the page loads, submits the form and then redirects the user back to my own Home page.  The reason for that bit of hoodoo is that I\'m launching the external site in a new window, but don\'t want the user to resubmit the hidden form if they refresh the page.  Plus that hidden form pushed the page down a few pixels which got on my nerves.</p>\n\n<p>I\'d be very interested in any cleaner ideas anyone has on this one.</p>\n\n<p>Eric Sipple</p>\n'}, {'answer_id': 5614, 'author': 'sestocker', 'author_id': 285, 'author_profile': 'https://Stackoverflow.com/users/285', 'pm_score': 3, 'selected': False, 'text': '<p>I would do the form post in your code behind using HttpWebRequest class. Here is a good helper class to get your started:</p>\n<p>&lt;<a href="https://web.archive.org/web/20210123193021/http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx" rel="nofollow noreferrer">Link</a>&gt;</p>\n<p>From there, you can just do a Response.Redirect, or perhaps you need to vary your action based on the outcome of the post (if there was an error, display it to the user or whatever). I think you already had the answer in your question to be honest - sounds like you think it is a post OR redirect when in reality you can do them both from your code behind.</p>\n'}, {'answer_id': 2112168, 'author': 'Kevin', 'author_id': 153942, 'author_profile': 'https://Stackoverflow.com/users/153942', 'pm_score': 3, 'selected': False, 'text': '<p>I started with this example from <a href="http://www.codeproject.com/KB/aspnet/ASP_NETRedirectAndPost.aspx" rel="nofollow noreferrer">CodeProject</a></p>\n\n<p>Then instead of adding to the page, I borrowed from saalon (above) and did a Response.Write().</p>\n'}, {'answer_id': 18473342, 'author': 'Jenn', 'author_id': 818004, 'author_profile': 'https://Stackoverflow.com/users/818004', 'pm_score': 2, 'selected': False, 'text': '<p>I needed to open in the same window, dealing with possible frame issues from the original page, then redirecting to an external site in code behind:</p>\n\n<pre><code>Private Sub ExternalRedirector(ByVal externalUrl As String)\n    Dim clientRedirectName As String = "ClientExternalRedirect"\n    Dim externalRedirectJS As New StringBuilder()\n\n    If Not String.IsNullOrEmpty(externalUrl) Then\n        If Not Page.ClientScript.IsStartupScriptRegistered(clientRedirectName) Then\n            externalRedirectJS.Append("function CheckWindow() {")\n            externalRedirectJS.Append("   if (window.top != window) {")\n            externalRedirectJS.Append("       window.top.location = \'")\n            externalRedirectJS.Append(externalUrl)\n            externalRedirectJS.Append("\';")\n            externalRedirectJS.Append("       return false;")\n            externalRedirectJS.Append("   }")\n            externalRedirectJS.Append("   else {")\n            externalRedirectJS.Append("   window.location = \'")\n            externalRedirectJS.Append(externalUrl)\n            externalRedirectJS.Append("\';")\n            externalRedirectJS.Append("   }")\n            externalRedirectJS.Append("}")\n            externalRedirectJS.Append("CheckWindow();")\n\n            Page.ClientScript.RegisterStartupScript(Page.GetType(), clientRedirectName, externalRedirectJS.ToString(), True)\n        End If\n    End If\nEnd Sub\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/5179', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/111/']