locked
Posting ASP.NET web page to another URL RRS feed

  • Question

  • User-562119727 posted

    Hi,

    I need to make FORM POST from my ASP.NET WebForms page to another external site URL. The form should POST the requests to the given URL, sending parameters together with the POST.

    Here I have created a form which has the Username field. When the user enters the username and click signin button the Form should be posted to third party url along with the username. Then according to the entered username, thrid party password page should open up.

    The issue is, when I enter the user name and click signin, form is posted to the username page rather than the password page. I have enclosed the sample code here. Please do help me in this.

    Login.aspx
    
    <body>
        <form id="form1" runat="server">
        <div>
            <p>
                Username:&nbsp;&nbsp;&nbsp;<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="btnSignin" runat="server" 
                    Text="Sign In" onclick="btnSignin_Click" />
            </p>
        </div>
        </form>
    </body>
    Login.aspx.cs
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Configuration;
    using System.Text.RegularExpressions;
    using System.Web.UI.WebControls;
    
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void btnSignin_Click(object sender, EventArgs e)
        {
            string formid = "AccessForm";
            string url = "https://www.SampleTest.org/User/AccessSignin/Password";
            StringBuilder htmlform = new StringBuilder();
            htmlform.AppendLine("<html>");
            htmlform.AppendLine("<head><title></title></head>");
            htmlform.AppendLine(String.Format("<body onload='document.forms[\"{0}\"].submit()'>", formid));
            htmlform.AppendLine(String.Format("<FORM id='{0}'  METHOD='POST' ACTION='{1}'>", formid, url));
            htmlform.AppendLine(String.Format("<input type='hidden' name='UsernameField' value='{0}'>", Server.HtmlEncode(txtUsername.Text)));
            htmlform.AppendLine("<input type='submit' name='SubmitNext' value='LogIn' style='display: none' />");
            //htmlform.AppendLine("<script>document.AccessForm.submit()</script>");
            htmlform.AppendLine("</form>");
            htmlform.AppendLine("</body>");
            htmlform.AppendLine("</html>");
    
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(htmlform.ToString());
            HttpContext.Current.Response.End();
        }
    }

    Thanks.

    Wednesday, February 29, 2012 12:59 PM

All replies

  • User-188060501 posted

    Hi,

    What you are trying to do, is what is called a crosspage post.

    This has been hard in previous versions of .NET, with .NET 4.0 it is simple.

    As I see it you are actually recreating your form in codebehind before sending it to the target URL. This should not be necessary, and I would guess that this is what is causing your solution to err.

    On the relevant button you can specify the PostBackUrl and omit your button click code:

    <asp:Button ID="btnSignin" runat="server" Text="Submit page to Page2.aspx" PostBackUrl="https://www.SampleTest.org/User/AccessSignin/Password" />

    Br. Tonny


    Wednesday, February 29, 2012 1:49 PM