如何发布,然后重定向到从ASP.Net外部URL?

问题描述:

ASP.NET服务器端控件回发到自己的页面。这使得要在其中将用户重定向到一个外部网页,但需要张贴到网页出于某种原因(用于身份验证,例如)疼痛。

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.

这是的HttpWebRequest 的伟大工程,如果你不希望重定向和JavaScript是罚款在某些情况下,但可能很麻烦,如果你真的需要在服务器端code一起获得的数据为后。

An HttpWebRequest 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.

那么,如何做你俩后到一个外部URL,并从你的ASP.NET codebehind code将用户重定向到的结果?

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

下面是我今天解决了这个问题。我从this对C#角文章,但发现的例子 - 虽然技术上声音 - 有点不完整的。他所说的一切是正确的,但我需要打了几个外部网站拼凑了一起,因为我想确切地工作。

Here's I solved this problem today. I started from this article 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.

这并没有帮助,用户没有在技术上提交表单在所有;他们点击一个链接转到我们的支持中心,但在一个HTTP POST来登录他们不得不支持中心的网站上进行。

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.

该解决方案包括使用HttpContext.Current.Response.Write()来写入数据的格式,然后用一些JavaScript的

This solution involves using HttpContext.Current.Response.Write() to write the data for the form, then using a bit of Javascript on the

<body onload=""> 

\r
\r

方法的表单提交到正确的URL。

method to submit the form to the proper URL.

在支持中心链接,用户点击,下面的方法被调用写响应和用户重定向:

When the user clicks on the Support Center link, the following method is called to write the response and redirect the user:

public static void PassthroughAuthentication()
{

System.Web.HttpContext.Current.Response.Write("<body
onload=document.forms[0].submit();window.location=\"Home.aspx\";>");

System.Web.HttpContext.Current.Response.Write("<form name=\"Form\"
target=_blank method=post
action=\"https://external-url.com/security.asp\">");

System.Web.HttpContext.Current.Response.Write(string.Format("<input
type=hidden name=\"cFName\" value=\"{0}\">", "Username"));

System.Web.HttpContext.Current.Response.Write("</form>");
System.Web.HttpContext.Current.Response.Write("</body>");
}

\r
\r

这个方法的关键是在Javascript中,其中,当页面加载的身体,提交表单的是onload事件位,然后将用户重定向回我自己的主页。原因石林的那一点是我要推出一个新的窗口,外部网站,但不希望用户重新提交隐藏的窗体,如果他们刷新页面。加上隐藏的窗体推页面下降,这得到了我的神经几个像素。

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.

我会在任何清洁剂的想法任何人有这个很感兴趣。

I'd be very interested in any cleaner ideas anyone has on this one.

埃里克Sipple