Community discussion forum

Managing OnAuthenticate event of asp:login in code-behind [ASP.NET 2.0]

  • 1 year ago

    I have a page [default.aspx] which associated code-behind [default.aspx.cs].
    In my default.aspx page I am using the ASP.NET Login Control and want to be able to manage the authentication event myself by doing the following:

    [Code]
     <asp:loginview id="LoginArea" runat="server">
      <AnonymousTemplate>
       <asp:login id="Login1" OnAuthenticate="OnAuthenticate" OnLoggedIn="OnLoggedIn" DestinationPageUrl="~/Clients.aspx" runat="server">
        <layouttemplate>
         <div class="login">
                                  <h4>Login to Site</h4>
                                  <asp:label runat="server" id="UserNameLabel" CssClass="label" associatedcontrolid="UserName">Card Number</asp:label>
                                  <asp:textbox runat="server" id="UserName" cssclass="textbox" accesskey="u" />
                                  <asp:requiredfieldvalidator runat="server" id="UserNameRequired" controltovalidate="UserName" validationgroup="Login1" errormessage="User Name is required." tooltip="User Name is required." >*</asp:requiredfieldvalidator>
                                  <asp:label runat="server" id="PasswordLabel" CssClass="label" associatedcontrolid="Password">Password</asp:label>
                                  <asp:textbox runat="server" id="Password" textmode="Password" cssclass="textbox" accesskey="p" />
                                  <asp:requiredfieldvalidator runat="server" id="PasswordRequired" controltovalidate="Password" validationgroup="Login1" tooltip="Password is required." >*</asp:requiredfieldvalidator>
                                  <div>
              <asp:checkbox runat="server" id="RememberMe" text="Remember me next time"/>
                                  </div>
                                  <asp:imagebutton runat="server" id="LoginButton" CommandName="Login" AlternateText="login" skinid="login" CssClass="button"/>
                                  <p><asp:literal runat="server" id="FailureText" enableviewstate="False"></asp:literal></p>
         </div>
        </layouttemplate>
       </asp:login>
      </AnonymousTemplate>
      <LoggedInTemplate>
       <h4><asp:loginname id="LoginName1" runat="server" formatstring="Welcome {0}!" /></h4>
      </LoggedInTemplate>
     </asp:loginview>
    [/Code]

     

    In the code-behind [default.aspx.cs] I have added the following function within (public partial class Default_aspx : System.Web.UI.Page {...})

    [Code]
        protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
        {
     string sUser = Login1.UserName;
     string sPwrd = Login1.Password;
     PerformCustomerAuthentication(sUser, sPwrd);
        }
    [/Code]

    This approach generates the following error:
    --> "The name 'Login1' does not exist in the current context"
    (for both lines trying to access Login1)

     

    I did some testing and if I remove pretty much everything from my asp:login control (in default.aspx) it seems to work fine ... but then I loose a lot of the control I was looking for... For example if I simply do the following it works:

    [Code]
    <asp:Content ID="c" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
            <asp:login ID="Login1" runat="server" OnAuthenticate="OnAuthenticate"></asp:login>
    </asp:Content>
    [/Code]


    Any clue where my implementation is going wrong or how I can fix it? At this point I am pretty much stuck ...
    Any help would be greatly appreciated...
    Thanks,

  • 1 year ago

     Hey! I think you've gotten a little side-tracked by the whole login stuff! The reason you can't access "Login1" is because it's within the template of another INamingContainer (your loginview control). Just as in a repeater you couldn't access server controls within it's ItemTemplate from the code behind.

    Your easiest solution in this instance is probably to cast the sender parameter to your event handler back to the Login control (as you know that will be the sender). Something like

         protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
        {

    LoginControl ctrl = sender as LoginControl;
    if (ctrl!=null) {
     string sUser = Login1.UserName;
     string sPwrd = Login1.Password;
     PerformCustomerAuthentication(sUser, sPwrd);
    }
        }

     

    Hope that helps!

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback