saa*_*lon 34 javascript c# forms asp.net postback
ASP.NET服务器端控件回发到自己的页面.这使得您希望将用户重定向到外部页面的情况,但由于某种原因(例如,用于身份验证)需要发布到该页面是一种痛苦.
一HttpWebRequest,如果你不想重定向和JavaScript是在某些情况下罚款,但可能很麻烦,如果你真的需要在服务器端代码一起获得数据后的伟大工程.
那么如何发布外部URL并将用户重定向到ASP.NET代码隐藏代码的结果?
saa*_*lon 13
这是我今天解决了这个问题.我从这篇关于C#Corner的文章开始,但发现这个例子 - 虽然技术上合理 - 但有点不完整.他说的一切都是对的,但是我需要打几个外部网站把它拼凑起来,完全按照我的意愿工作.
用户在技术上根本没有提交表格并没有帮助; 他们点击链接转到我们的支持中心,但是要将它们记录在http帖子中,必须进入支持中心的网站.
这个解决方案涉及使用HttpContext.Current.Response.Write()来编写表单的数据,然后使用一些Javascript
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>");
}
Run Code Online (Sandbox Code Playgroud)
将表单提交到正确的URL的方法.
当用户单击支持中心链接时,将调用以下方法来编写响应并重定向用户:
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>");
}
Run Code Online (Sandbox Code Playgroud)
这个方法的关键在于Javascript的onload位,当页面正文加载时,它会提交表单,然后将用户重定向回我自己的主页.这一点不足的原因是我在一个新窗口中启动了外部网站,但是如果他们刷新页面,则不希望用户重新提交隐藏的表单.再加上隐藏的形式将页面向下推了几个像素,这让我更加紧张.
我会对任何人在这个问题上有任何更清晰的想法感兴趣.
Eric Sipple