重定向到另一个网站时使用隐藏字段

问题描述 投票:0回答:0

使用 C# Web 表单,我尝试使用隐藏字段作为从网站 A 到网站 B 的重定向的一部分(相同的域,两个网站都使用表单身份验证进行密码保护)。我尝试了不同的方法,但到目前为止,我无法从网站 B Login 代码隐藏中的 Request.Form NameValueCollection 中提取隐藏字段值,并且在尝试访问请求表单密钥时收到空引用异常。

网站A:

<asp:LinkButton ID="redirectLink" runat="server" OnClick="RedirectLink_Click"
OnClientClick="window.document.forms[0].target='_blank';">New Page - Website B
</asp:LinkButton>

Response.Clear();
StringBuilder sb = new StringBuilder();

sb.Append("<html>");
sb.Append("<body onload='submitForm()'>");
sb.AppendFormat("<form id='customForm' action='{0}' method='post'>", url + "/newpage.aspx?redirectPage=NewPage_Redirect");
sb.AppendFormat("<input type='hidden' name='Val1' value='{0}' id='Val1'>", Uri.EscapeDataString(value1));
sb.AppendFormat("<input type='hidden' name='Val2' value='{0}' id='Val2'>", Uri.EscapeDataString(value2));
sb.Append("</form>");
sb.Append("<script>");
sb.Append("function submitForm() {");
sb.Append("document.getElementById('customForm').submit();");
sb.Append("}");
sb.Append("</script>");
sb.Append("</body>");
sb.Append("</html>");
Response.Write(sb.ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();

网站B Page_Load:

if ((Request.QueryString["redirectPage"] != null)          
    && (Request.QueryString["redirectPage"].ToString() == "NewPage_Redirect"))
{
    Response.Write(Request.Form["Val1"].ToString()); // NULL REFERENCE EXCEPTION
    Response.Write(Request.Form["Val2"].ToString());
}

我错过了什么?

更新1

网站A:

这是我在 VS 2022 中看到的 HTML:

<html>
<body onload='submitForm()'>
<form id='customForm' action='https://websiteB.com/newpage.aspx?redirectPage=NewPage_Redirect' method='post'>
    <input type='hidden' name='Val1' value='abc123' id='Val1'>
    <input type='hidden' name='Val2' value='def456' id='Val2'>
</form>
<script>
    function submitForm() 
    {
        document.getElementById('customForm').submit();
    }
</script>
</body>
</html>

网站B Page_Load:

当我将网站 B 上的 Response.Write(Request.Form["Val1"].ToString()) 更改为 Response.Write(Request.Form["Val1"]) 时,我不再遇到空引用异常。因此,我将 B 网站上的测试代码替换为以下内容,其中键的长度显示为 0:

string[] keys = Request.Form.AllKeys;
Response.Write(keys.Length.ToString()); //Displays 0 

更新2:

使用浏览器工具,代码中创建的 HTML 似乎没有出现在生成的 HTML 中。我开始怀疑这可能是一个问题我在 12 年前处理过,其中 ASP.NET Web 表单不允许嵌套表单。仍在调查中。

更新3

我忘了提及我正在 ASPX 页面上使用 LinkButton - 上面的代码片段已更新。

asp.net http redirect webforms forms-authentication
© www.soinside.com 2019 - 2024. All rights reserved.