如何发布并重定向到来自ASP.Net的外部URL ?

时间:2022-06-12 04:09:33

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.

ASP。NET服务器端控件回发到自己的页面。这使得您想要将用户重定向到外部页面,但是由于某些原因(例如为了身份验证)需要发布到该页面时非常痛苦。

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.

如果您不希望重定向,HttpWebRequest是非常有用的,而且JavaScript在某些情况下是可以的,但是如果您确实需要服务器端代码来收集post的数据,那么JavaScript就会变得很棘手。

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

因此,如何将两个post发送到外部URL并将用户重定向到来自ASP的结果。网后台代码代码?

6 个解决方案

#1


9  

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.

这是我今天解决的问题。我从这篇关于c# Corner的文章开始,但是发现这个例子有点不完整,虽然从技术上来说还不错。他说的每句话都是对的,但我需要访问一些外部站点,将其整合到一起,以实现我想要的效果。

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.

从技术上讲,用户根本就没有提交表单;他们点击了一个链接到我们的支持中心,但是要把他们记录在一个http的帖子里,就必须被放到支持中心的网站上。

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

这个解决方案涉及使用HttpContext.Current.Response.Write()来为表单编写数据,然后在表单上使用一点Javascript

<body onload=""> 

method to submit the form to the proper URL.

方法将表单提交到适当的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>");
}

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.

该方法的关键在于Javascript的onload位,当页面主体加载时,该位提交表单,然后将用户重定向回我自己的主页。这一点的原因是,我在一个新窗口中启动了外部站点,但不希望用户在刷新页面时重新提交隐藏的表单。再加上那张隐藏的表格,把我的神经压了下来。

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

我对任何人在这个问题上有任何更清晰的想法都很感兴趣。

Eric Sipple

Eric Sipple

#2


4  

I started with this example from CodeProject

我从CodeProject这个例子开始

Then instead of adding to the page, I borrowed from saalon (above) and did a Response.Write().

然后,我从saalon(上面)借用了saalon(上面)并做了一个Response.Write()。

#3


2  

I would do the form post in your code behind using HttpWebRequest class. Here is a good helper class to get your started:

我将在您的代码后面使用HttpWebRequest类执行表单post。这里有一个很好的帮助类来开始你的课程:

http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx

http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx

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.

从那里,你可以做一个回应。重定向,或者您可能需要根据post的结果更改操作(如果有错误,将其显示给用户或其他)。我认为你已经在你的问题中得到了答案——听起来像你认为它是一个职位或重定向,实际上你可以从你的代码中完成它们。

#4


1  

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.

我通过呈现一个表单自动发布(使用JavaScript)到所需的远程URL——在web表单的回发中收集post所需的任何信息,然后为远程发布表单构建HTML并将其呈现给客户端。

I built a utility class for this that contains the remote URL and a collection of name/value pairs for the form.

我为此构建了一个实用程序类,其中包含远程URL和表单的名称/值对集合。

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).

如果您同时拥有这两个页面,那么跨页面发布将有效,但如果您需要向另一个站点(例如贝宝)发布,则不能。

#5


1  

If you're using ASP.NET 2.0, you can do this with cross-page posting.

如果你使用ASP。NET 2.0,你可以通过跨页发布来实现这一点。

Edit: I missed the fact that you're asking about an external 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.

编辑:我没注意到你问的是一个外部页面。我认为你需要使用ASP。NET页面生成一个HTML表单,其动作设置为远程URL,方法设置为POST。(使用跨页发布,这甚至可以是一个没有UI、只有隐藏表单元素的不同页面。)然后在客户机上收到回发结果后,添加一点javascript来提交表单。

#6


1  

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:

我需要在同一个窗口中打开,处理原始页面可能出现的框架问题,然后在代码后面重定向到一个外部站点:

Private Sub ExternalRedirector(ByVal externalUrl As String)
    Dim clientRedirectName As String = "ClientExternalRedirect"
    Dim externalRedirectJS As New StringBuilder()

    If Not String.IsNullOrEmpty(externalUrl) Then
        If Not Page.ClientScript.IsStartupScriptRegistered(clientRedirectName) Then
            externalRedirectJS.Append("function CheckWindow() {")
            externalRedirectJS.Append("   if (window.top != window) {")
            externalRedirectJS.Append("       window.top.location = '")
            externalRedirectJS.Append(externalUrl)
            externalRedirectJS.Append("';")
            externalRedirectJS.Append("       return false;")
            externalRedirectJS.Append("   }")
            externalRedirectJS.Append("   else {")
            externalRedirectJS.Append("   window.location = '")
            externalRedirectJS.Append(externalUrl)
            externalRedirectJS.Append("';")
            externalRedirectJS.Append("   }")
            externalRedirectJS.Append("}")
            externalRedirectJS.Append("CheckWindow();")

            Page.ClientScript.RegisterStartupScript(Page.GetType(), clientRedirectName, externalRedirectJS.ToString(), True)
        End If
    End If
End Sub

#1


9  

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.

这是我今天解决的问题。我从这篇关于c# Corner的文章开始,但是发现这个例子有点不完整,虽然从技术上来说还不错。他说的每句话都是对的,但我需要访问一些外部站点,将其整合到一起,以实现我想要的效果。

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.

从技术上讲,用户根本就没有提交表单;他们点击了一个链接到我们的支持中心,但是要把他们记录在一个http的帖子里,就必须被放到支持中心的网站上。

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

这个解决方案涉及使用HttpContext.Current.Response.Write()来为表单编写数据,然后在表单上使用一点Javascript

<body onload=""> 

method to submit the form to the proper URL.

方法将表单提交到适当的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>");
}

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.

该方法的关键在于Javascript的onload位,当页面主体加载时,该位提交表单,然后将用户重定向回我自己的主页。这一点的原因是,我在一个新窗口中启动了外部站点,但不希望用户在刷新页面时重新提交隐藏的表单。再加上那张隐藏的表格,把我的神经压了下来。

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

我对任何人在这个问题上有任何更清晰的想法都很感兴趣。

Eric Sipple

Eric Sipple

#2


4  

I started with this example from CodeProject

我从CodeProject这个例子开始

Then instead of adding to the page, I borrowed from saalon (above) and did a Response.Write().

然后,我从saalon(上面)借用了saalon(上面)并做了一个Response.Write()。

#3


2  

I would do the form post in your code behind using HttpWebRequest class. Here is a good helper class to get your started:

我将在您的代码后面使用HttpWebRequest类执行表单post。这里有一个很好的帮助类来开始你的课程:

http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx

http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx

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.

从那里,你可以做一个回应。重定向,或者您可能需要根据post的结果更改操作(如果有错误,将其显示给用户或其他)。我认为你已经在你的问题中得到了答案——听起来像你认为它是一个职位或重定向,实际上你可以从你的代码中完成它们。

#4


1  

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.

我通过呈现一个表单自动发布(使用JavaScript)到所需的远程URL——在web表单的回发中收集post所需的任何信息,然后为远程发布表单构建HTML并将其呈现给客户端。

I built a utility class for this that contains the remote URL and a collection of name/value pairs for the form.

我为此构建了一个实用程序类,其中包含远程URL和表单的名称/值对集合。

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).

如果您同时拥有这两个页面,那么跨页面发布将有效,但如果您需要向另一个站点(例如贝宝)发布,则不能。

#5


1  

If you're using ASP.NET 2.0, you can do this with cross-page posting.

如果你使用ASP。NET 2.0,你可以通过跨页发布来实现这一点。

Edit: I missed the fact that you're asking about an external 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.

编辑:我没注意到你问的是一个外部页面。我认为你需要使用ASP。NET页面生成一个HTML表单,其动作设置为远程URL,方法设置为POST。(使用跨页发布,这甚至可以是一个没有UI、只有隐藏表单元素的不同页面。)然后在客户机上收到回发结果后,添加一点javascript来提交表单。

#6


1  

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:

我需要在同一个窗口中打开,处理原始页面可能出现的框架问题,然后在代码后面重定向到一个外部站点:

Private Sub ExternalRedirector(ByVal externalUrl As String)
    Dim clientRedirectName As String = "ClientExternalRedirect"
    Dim externalRedirectJS As New StringBuilder()

    If Not String.IsNullOrEmpty(externalUrl) Then
        If Not Page.ClientScript.IsStartupScriptRegistered(clientRedirectName) Then
            externalRedirectJS.Append("function CheckWindow() {")
            externalRedirectJS.Append("   if (window.top != window) {")
            externalRedirectJS.Append("       window.top.location = '")
            externalRedirectJS.Append(externalUrl)
            externalRedirectJS.Append("';")
            externalRedirectJS.Append("       return false;")
            externalRedirectJS.Append("   }")
            externalRedirectJS.Append("   else {")
            externalRedirectJS.Append("   window.location = '")
            externalRedirectJS.Append(externalUrl)
            externalRedirectJS.Append("';")
            externalRedirectJS.Append("   }")
            externalRedirectJS.Append("}")
            externalRedirectJS.Append("CheckWindow();")

            Page.ClientScript.RegisterStartupScript(Page.GetType(), clientRedirectName, externalRedirectJS.ToString(), True)
        End If
    End If
End Sub