I need to automatically log users in to an asp.net application who will be logging in from a php webpage. I thought this would be fairly simple but am having problems getting it to work. I set up a remote test page that submits an emai and password to the login page. On the login page I added the following code -
<script runat="server">
void Page_Load(object sender, EventArgs e){
if (IsPostBack) {
// It is a postback
} else {
// It is not a postback
Session["email"]= Request.Form["email"];
Session["password"]= Request.Form["password"];
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "submitForm()", true);
}
</script>
<script type="text/javascript">
function submitForm() {
document.forms["form1"].elements["email"].value = "<%= Session["email"] %>";
document.forms["form1"].password.value = "<%= Session["password"] %>";
document.forms["form1"].submit();
}
</script>
If I remove the document.forms["form1"].submit(); line I am taken to the login page and the email and password are populated in the appropriate boxes, if I then click the submit button manually I get logged in. As a test I then removed only the document.forms["form1"].password.value = "<%= Session["password"] %>"; line and it seemed to submit the form as I got a validation error saying that a password is required (as I would do submitting the login manually without a password)
However as it stands it takes me to the login page and only the email is populated, I assume both email and password have been sent and then email has dissapeared after summission but don't know why that would be the case (given that a manually submission works) am I missing something obvious?
Thanks
Form markup is -
<form id="form1" runat="server">
<table class="loginpanelouter" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td class="loginlogo" colspan="2">
<asp:Image ID="CMSLogo" runat="server" />
</td>
</tr>
<tr>
<td class="logininvalid" colspan="2">
<asp:Label ID="ErrorMessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="loginforgot" colspan="2">
<asp:LinkButton ID="ForgottenPassBtn" CausesValidation="false" runat="server">Forgotten Password?</asp:LinkButton>
</td>
</tr>
<tr>
<td class="logintext">
Email:
</td>
<td valign="middle" width="200">
<asp:TextBox ID="email" CssClass="loginpagefields" runat="server" size="25"></asp:TextBox>
</td>
</tr>
<tr>
<td class="logintext">
</td>
<td width="240" valign="middle">
<asp:RegularExpressionValidator ID="emailValid" runat="server" ValidationExpression="[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+"
ErrorMessage="Please enter a valid email address." ControlToValidate="email"
EnableClientScript="False"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="logintext">
Password:
</td>
<td width="200" valign="middle">
<asp:TextBox ID="password" CssClass="loginpagefields" runat="server" size="25" TextMode="password"></asp:TextBox>
</td>
</tr>
<tr>
<td class="logintext">
</td>
<td width="240" valign="middle">
<asp:RequiredFieldValidator ID="passwordRequired" runat="server" ErrorMessage="Please enter your Password"
Display="Static" ControlToValidate="password" EnableClientScript="False"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<table>
<tr>
<td class="loginremeber">
<asp:CheckBox ID="RememberLogin" runat="server" Text="Remember my sign in details"
ToolTip="Please select this box if you wish your sign in details to be remembered">
</asp:CheckBox>
</td>
<td width="120px" valign="middle" align="right">
<asp:Button ID="LoginBtn" runat="server" CssClass="buttonstyle" Text="Sign in" ToolTip="Please click to sign in">
</asp:Button>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" height="35">
</td>
</tr>
</table>
</form>
formmarkup look like? Why are you accessing theemailfield one way (.elements["email"]) and thepasswordfield another way (.password)?"in either my email or password, the script will break. Similarly, if I have a backslash in either field (perhaps I'm using the password "foo\bar"), the field will get the wrong value (as\bin a string literal is a backspace).