views:

2884

answers:

2

I've got to put a login page from a 3rd party website in an iframe on another website that I'm developing. I need to add some JavaScript to break out of the iframe when the user logs in, but I can't make the login button to execute the JavaScript and do the postback for the login - just one or the other.

Here's the code from the iframe'd login page that I'm triying to adapt:

<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
 <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
  <LayoutTemplate>
    <asp:Label ID="lblUsername" runat="server" AssociatedControlID="Username" Text="Email" />
    <asp:TextBox ID="Username" runat="server" Text="myName" />
    <asp:Label ID="lblPassword" runat="server" AssociatedControlID="Password" Text="Password" />
    <asp:TextBox ID="Password" runat="server" Text="myPassword" />
    <asp:ImageButton ID="btnLogin" runat="server" CommandName="Login" ImageUrl="~/Images/login-submit.gif" AlternateText="Login" OnClientClick="top.location.href = document.location.href; return true;" />
  </LayoutTemplate>
 </asp:Login>
</AnonymousTemplate>
<LoggedInTemplate>
 You are currently logged in blurb..
</LoggedInTemplate>

Currently when the login button is clicked, the login page breaks out of the iframe, but then I have to click the button again to log the user in. Can anyone see what I'm doing wrong? Thanks.

+4  A: 

top.location.href is setting the url of the browser to be a new url so you are never completing the action of the click.

What you could do is set the target of the form to be "_top"

something like

OnClientClick="document.getElementById('MYFORM_CLIENTID').target='_top';return true;"
Justin Wignall
Perfect, thanks very much.
Nick
Yeah, that should work!
Pawel Krakowiak
+1  A: 

Looking at your code I think that on the first click you are not logging in. You are just redirecting the parent window to your login page (the one which is displayed in the IFrame at first). That is why you need to click that button twice to log in - on the second time there is no redirect, as the parent window's URL does not change, so basically on the second time the top.location.href = document.location.href part does not yield any results and the login proceeds.

I think that the right course of action would be to authenticate first, then redirect, the opposite of how it looks currently. You can add a script on postback on that page, so that it checks if it's running in a frame and if so, it redirects the parent window to another page. Of course if you can modify the code... With the amount of detail that was provided I'm only able to tell what may be wrong and suggest a fix, but can't give a solution.

Pawel Krakowiak
Unfortunately I only have access to the .aspx in this situation, not the .cs file. If their login page accepted posted values from my site I wouldn't need the iframe / javascript nonsense.. :)
Nick
Justin came up with the simplest solution anyway. I at least managed to identify the problem cause...
Pawel Krakowiak