admin管理员组

文章数量:1334183

I need to automatically log users in to an asp 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:&nbsp;&nbsp;
            </td>
            <td valign="middle" width="200">
                <asp:TextBox ID="email" CssClass="loginpagefields" runat="server" size="25"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="logintext">
                &nbsp;
            </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:&nbsp;&nbsp;
            </td>
            <td width="200" valign="middle">
                <asp:TextBox ID="password" CssClass="loginpagefields" runat="server" size="25" TextMode="password"></asp:TextBox>&nbsp;&nbsp;&nbsp;
            </td>
        </tr>
        <tr>
            <td class="logintext">
                &nbsp;
            </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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        </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">
                &nbsp;
            </td>
        </tr>
    </table>
    </form>

I need to automatically log users in to an asp 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:&nbsp;&nbsp;
            </td>
            <td valign="middle" width="200">
                <asp:TextBox ID="email" CssClass="loginpagefields" runat="server" size="25"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="logintext">
                &nbsp;
            </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:&nbsp;&nbsp;
            </td>
            <td width="200" valign="middle">
                <asp:TextBox ID="password" CssClass="loginpagefields" runat="server" size="25" TextMode="password"></asp:TextBox>&nbsp;&nbsp;&nbsp;
            </td>
        </tr>
        <tr>
            <td class="logintext">
                &nbsp;
            </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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        </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">
                &nbsp;
            </td>
        </tr>
    </table>
    </form>
Share Improve this question edited Mar 20, 2012 at 16:25 Martin McMahon asked Mar 20, 2012 at 9:56 Martin McMahonMartin McMahon 3331 gold badge7 silver badges22 bronze badges 3
  • What does your form markup look like? Why are you accessing the email field one way (.elements["email"]) and the password field another way (.password)? – T.J. Crowder Commented Mar 20, 2012 at 10:02
  • Side note: There are several problems with echoing back the email and password that way, not least that you're echoing back a password in plain text. Also, when you output server-side values within JavaScript string literals, you have to make sure that what you'e outputting will always be valid contents for a string literal. As your code stands now, if I have a " 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 \b in a string literal is a backspace). – T.J. Crowder Commented Mar 20, 2012 at 10:06
  • I've pasted form markup above. I've also made the email consistent with the password but it made no difference. Thanks – Martin McMahon Commented Mar 20, 2012 at 16:26
Add a ment  | 

2 Answers 2

Reset to default 4

I managed to get it to work.

For some reason it didn't work with -

document.forms["form1"].submit();

But changing this to -

document.forms["form1"].elements["LoginBtn"].click();

Now lets me login.

The reason it didn't work with submit is due to the fact you had not Input Type="Submit". An asp:button is not necessarily a "Submit" button, it is rendered as a button that performs an action. A Submit button performs a POST to the FORM'S ACTION PAGE. If no action page is defined (which is the case in your sample) nothing happens.

本文标签: Automatically submitting an aspnet form with javascriptStack Overflow