[asp.net] How to POST a FORM from HTML to ASPX page

How do I post a form from an HTML page to and ASPX page (2.0) and be able to read the values?

I currently have an ASP.NET site using the Membership provider and everything is working fine. Users can log in from the Login.aspx page.

We now want to be able to have users log in directly from another web site--which is basically a static HTML page. The users need to be able to enter their name and password on this HTML page and have it POST to my Login.aspx page (where I can then log them in manually).

Is it possible to pass form values from HTML to ASPX? I have tried everything and the Request.Form.Keys collection is always empty. I can't use a HTTP GET as these are credentials and can't be passed on a query string.

The only way I know of is an iframe.

This question is related to asp.net

The answer is


Are you sure your HTML form is correct, and does, in fact, do an HTTP POST? I would suggest running Fiddler2, and then trying to log in via your Login.aspx, then the remote HTML site, and then comparing the requests that are sent to the server. For me, ASP.Net always worked fine -- if HTTP request contains a valid POST, I can get to values using Request.Form...


This is very possible. I mocked up 3 pages which should give you a proof of concept:

.aspx page:

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox TextMode="password" ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
</form>

code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For Each s As String In Request.Form.AllKeys
        Response.Write(s & ": " & Request.Form(s) & "<br />")
    Next
End Sub

Separate HTML page:

<form action="http://localhost/MyTestApp/Default.aspx" method="post">
    <input name="TextBox1" type="text" value="" id="TextBox1" />
    <input name="TextBox2" type="password" id="TextBox2" />
    <input type="submit" name="Button1" value="Button" id="Button1" />
</form>

...and it regurgitates the form values as expected. If this isn't working, as others suggested, use a traffic analysis tool (fiddler, ethereal), because something probably isn't going where you're expecting.


The Request.Form.Keys collection will be empty if none of your html inputs have NAMEs. It's easy to forget to put them there after you've been doing .NET for a while. Just name them and you'll be good to go.


Are you sure your HTML form is correct, and does, in fact, do an HTTP POST? I would suggest running Fiddler2, and then trying to log in via your Login.aspx, then the remote HTML site, and then comparing the requests that are sent to the server. For me, ASP.Net always worked fine -- if HTTP request contains a valid POST, I can get to values using Request.Form...


You sure can.

The easiest way to see how you might do this is to browse to the aspx page you want to post to. Then save the source of that page as HTML. Change the action of the form on your new html page to point back to the aspx page you originally copied it from.

Add value tags to your form fields and put the data you want in there, then open the page and hit the submit button.


Are you sure your HTML form is correct, and does, in fact, do an HTTP POST? I would suggest running Fiddler2, and then trying to log in via your Login.aspx, then the remote HTML site, and then comparing the requests that are sent to the server. For me, ASP.Net always worked fine -- if HTTP request contains a valid POST, I can get to values using Request.Form...


This is very possible. I mocked up 3 pages which should give you a proof of concept:

.aspx page:

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox TextMode="password" ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
</form>

code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For Each s As String In Request.Form.AllKeys
        Response.Write(s & ": " & Request.Form(s) & "<br />")
    Next
End Sub

Separate HTML page:

<form action="http://localhost/MyTestApp/Default.aspx" method="post">
    <input name="TextBox1" type="text" value="" id="TextBox1" />
    <input name="TextBox2" type="password" id="TextBox2" />
    <input type="submit" name="Button1" value="Button" id="Button1" />
</form>

...and it regurgitates the form values as expected. If this isn't working, as others suggested, use a traffic analysis tool (fiddler, ethereal), because something probably isn't going where you're expecting.


The Request.Form.Keys collection will be empty if none of your html inputs have NAMEs. It's easy to forget to put them there after you've been doing .NET for a while. Just name them and you'll be good to go.


This is very possible. I mocked up 3 pages which should give you a proof of concept:

.aspx page:

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox TextMode="password" ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
</form>

code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For Each s As String In Request.Form.AllKeys
        Response.Write(s & ": " & Request.Form(s) & "<br />")
    Next
End Sub

Separate HTML page:

<form action="http://localhost/MyTestApp/Default.aspx" method="post">
    <input name="TextBox1" type="text" value="" id="TextBox1" />
    <input name="TextBox2" type="password" id="TextBox2" />
    <input type="submit" name="Button1" value="Button" id="Button1" />
</form>

...and it regurgitates the form values as expected. If this isn't working, as others suggested, use a traffic analysis tool (fiddler, ethereal), because something probably isn't going where you're expecting.


In the html form, you need to supply additional viewstate variable and disable ViewState in a server page. This requires some control on both sides , though.

Form HTML:

<html><body> <form id='postForm' action='WebForm.aspx' method='POST'>
        <input type='text' name='postData' value='base-64-encoded-value' />
        <input type='hidden' name='__VIEWSTATE' value='' />  <!-- still need __VIEWSTATE, even empty one -->
    </form>

</body></html>

Note empty __VIEWSTATE.

WebForm.aspx:

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm.aspx.cs" Inherits="WebForm"
 EnableEventValidation="False" EnableViewState="false" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="postForm" runat="server">
        <asp:TextBox ID="postData" runat="server"></asp:TextBox>
    <div>

    </div>
    </form>
</body>
</html>

Note EnableEventValidation="False", EnableViewState="false" to prevent validation error for empty view state. Code Behind/Inherits values are not precise.

WebForm.cs:

public partial class WebForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string value = Encoding.Unicode.GetString(Convert.FromBase64String(this.postData.Text));
    }
}

You sure can.

The easiest way to see how you might do this is to browse to the aspx page you want to post to. Then save the source of that page as HTML. Change the action of the form on your new html page to point back to the aspx page you originally copied it from.

Add value tags to your form fields and put the data you want in there, then open the page and hit the submit button.


The Request.Form.Keys collection will be empty if none of your html inputs have NAMEs. It's easy to forget to put them there after you've been doing .NET for a while. Just name them and you'll be good to go.


This is very possible. I mocked up 3 pages which should give you a proof of concept:

.aspx page:

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox TextMode="password" ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
</form>

code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For Each s As String In Request.Form.AllKeys
        Response.Write(s & ": " & Request.Form(s) & "<br />")
    Next
End Sub

Separate HTML page:

<form action="http://localhost/MyTestApp/Default.aspx" method="post">
    <input name="TextBox1" type="text" value="" id="TextBox1" />
    <input name="TextBox2" type="password" id="TextBox2" />
    <input type="submit" name="Button1" value="Button" id="Button1" />
</form>

...and it regurgitates the form values as expected. If this isn't working, as others suggested, use a traffic analysis tool (fiddler, ethereal), because something probably isn't going where you're expecting.


You sure can.

The easiest way to see how you might do this is to browse to the aspx page you want to post to. Then save the source of that page as HTML. Change the action of the form on your new html page to point back to the aspx page you originally copied it from.

Add value tags to your form fields and put the data you want in there, then open the page and hit the submit button.


Hope this will help - Put this tag in html and

remove your login.aspx design content..just write only page directive

and you will get the values in aspx page after submit button click like this- protected void Page_Load(object sender, EventArgs e) {

        if (!IsPostBack)
        {
            CompleteRegistration();
        }
    }

public void CompleteRegistration() {

        NameValueCollection nv = Request.Form;
        if (nv.Count != 0)
        {
            string strname = nv["txtbox1"];
            string strPwd = nv["txtbox2"];
        }
    }

You sure can. Create an HTML page with the form in it that will contain the necessary components from the login.aspx page (i.e. username, etc), and make sure they have the same IDs. For you action, make sure it's a post.

You might have to do some code on the login.aspx page in the Page_Load function to read the form (in the Request.Form object) and call the appropriate functions to log the user in, but other than that, you should have access to the form, and can do what you want with it.


The Request.Form.Keys collection will be empty if none of your html inputs have NAMEs. It's easy to forget to put them there after you've been doing .NET for a while. Just name them and you'll be good to go.


Hope this will help - Put this tag in html and

remove your login.aspx design content..just write only page directive

and you will get the values in aspx page after submit button click like this- protected void Page_Load(object sender, EventArgs e) {

        if (!IsPostBack)
        {
            CompleteRegistration();
        }
    }

public void CompleteRegistration() {

        NameValueCollection nv = Request.Form;
        if (nv.Count != 0)
        {
            string strname = nv["txtbox1"];
            string strPwd = nv["txtbox2"];
        }
    }

Remove runat="server" parts of data posting/posted .aspx page.


In the html form, you need to supply additional viewstate variable and disable ViewState in a server page. This requires some control on both sides , though.

Form HTML:

<html><body> <form id='postForm' action='WebForm.aspx' method='POST'>
        <input type='text' name='postData' value='base-64-encoded-value' />
        <input type='hidden' name='__VIEWSTATE' value='' />  <!-- still need __VIEWSTATE, even empty one -->
    </form>

</body></html>

Note empty __VIEWSTATE.

WebForm.aspx:

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm.aspx.cs" Inherits="WebForm"
 EnableEventValidation="False" EnableViewState="false" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="postForm" runat="server">
        <asp:TextBox ID="postData" runat="server"></asp:TextBox>
    <div>

    </div>
    </form>
</body>
</html>

Note EnableEventValidation="False", EnableViewState="false" to prevent validation error for empty view state. Code Behind/Inherits values are not precise.

WebForm.cs:

public partial class WebForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string value = Encoding.Unicode.GetString(Convert.FromBase64String(this.postData.Text));
    }
}

You sure can.

The easiest way to see how you might do this is to browse to the aspx page you want to post to. Then save the source of that page as HTML. Change the action of the form on your new html page to point back to the aspx page you originally copied it from.

Add value tags to your form fields and put the data you want in there, then open the page and hit the submit button.


Remove runat="server" parts of data posting/posted .aspx page.


You sure can. Create an HTML page with the form in it that will contain the necessary components from the login.aspx page (i.e. username, etc), and make sure they have the same IDs. For you action, make sure it's a post.

You might have to do some code on the login.aspx page in the Page_Load function to read the form (in the Request.Form object) and call the appropriate functions to log the user in, but other than that, you should have access to the form, and can do what you want with it.


Are you sure your HTML form is correct, and does, in fact, do an HTTP POST? I would suggest running Fiddler2, and then trying to log in via your Login.aspx, then the remote HTML site, and then comparing the requests that are sent to the server. For me, ASP.Net always worked fine -- if HTTP request contains a valid POST, I can get to values using Request.Form...