How to change the login hyper link to logout when successfully logged in

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samaira
    New Member
    • Oct 2011
    • 7

    How to change the login hyper link to logout when successfully logged in

    I have created a login page when the user logs in it will take to a specified page with the text "welcome Username" and logout link on the right.but am not getting any logout link but instead itz login link which functions as a logout link though.and no message as "welcome Username"

    Below is the code I have used:

    Code:
    <div class="loginDisplay">
      <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
        <AnonymousTemplate>
          [<a href="~/Account/Login.aspx" id="HeadLoginStatus" runat="server">Log In</a>]
        </AnonymousTemplate>
        <LoggedInTemplate>
          <span>Welcome</span>
          Welcome 
          <span class="bold">
            <asp:LoginName ID="HeadLoginName" runat="server" FormatString="Welcome {0}"/>
          </span>! 
          [<asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/Account/Login.aspx" />]
        </LoggedInTemplate>
      </asp:LoginView>
    </div>
    Last edited by Frinavale; Oct 11 '11, 02:33 PM. Reason: Added code tags. Please post code in code tags. Merged the double posted question.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    It sounds like the login process is not working properly.
    Could you please post the server-side code that you have for when the user logs in?

    Comment

    • samaira
      New Member
      • Oct 2011
      • 7

      #3
      The code I have send is in the master page.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I asked you to post the server-side code that is executed in order to log in the user. This will be the C# or the VB.NET code that authenticates the user. If you do not have this code, then that would explain why you're "login" method isn't working...and therefore the reason for why your "log-out" link is never showing up.

        -Frinny
        Last edited by Frinavale; Oct 11 '11, 04:05 PM.

        Comment

        • samaira
          New Member
          • Oct 2011
          • 7

          #5
          I haven't written any code on the backend.could u please help me out with the code.Thanks

          Code:
          protected void Page_Load(object sender, EventArgs e)
                  {            
                      //MembershipUser membershipUser = Membership.GetUser();
                      //if (membershipUser != null)
                      //{
                      //    string loggedinuser = Membership.GetUser().ToString();
                      //    //HeadLoginView.Controls = "Welcome " + loggedinuser;
                      //}
                      //else
                      //{
                      //    namelbl2 = "";
                      //}
          
          
          
                  }
          Last edited by Frinavale; Oct 12 '11, 12:56 PM. Reason: Added code tags. Please post code in code tags.

          Comment

          • samaira
            New Member
            • Oct 2011
            • 7

            #6
            This the code:login.aspx
            Code:
            <asp:Login ID="LoginUser" runat="server" EnableViewState="false" 
                RenderOuterTable="false" onauthenticate="LoginUser_Authenticate">
                    <LayoutTemplate>
                        <span class="failureNotification">
                            <asp:Literal ID="FailureText" runat="server"></asp:Literal>
                        </span>
                        <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
                             ValidationGroup="LoginUserValidationGroup"/>
                        <div class="accountInfo">
                            <fieldset class="login">
                                <legend>Account Information</legend>
                                <p>
                                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
                                    <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                                         CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                                </p>
                                <p>
                                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                                    <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                                         CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                                </p>
                                <p>
                                    <asp:CheckBox ID="RememberMe" runat="server"/>
                                    <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
                                </p>
                            </fieldset>
                            <p class="submitButton">
                                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup"/>
                            </p>
                        </div>
                    </LayoutTemplate>
                </asp:Login>




            login.aspx.cs
            Code:
             public partial class Login : System.Web.UI.Page
                {
            protected void Page_Load(object sender, EventArgs e)
                    {
                        RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
                    }
            
                    protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
                    {
                        OdbcConnection conn = new OdbcConnection("Dsn=PostgreSQL35W2;uid=root;pwd=aeiou123");
                        conn.Open();
                        OdbcCommand cmd = new OdbcCommand();
                        cmd.CommandText = string.Format( "select lower( user_id) as user_id,password from mdb.users where user_id ='{0}' and password ='{1}'",LoginUser.UserName,LoginUser.Password);
                        cmd.Connection = conn;
            
                        OdbcDataReader dr = cmd.ExecuteReader();
            
                        if (dr.HasRows)
                        {
                            e.Authenticated = true;
                            dr.Close();
                            cmd.CommandText = "select emp_number from mdb.emp_details where lower(official_email_id) = '" + LoginUser.UserName + "'";
                            var emp_number = cmd.ExecuteScalar();
                            Session["EmployeeID"] = emp_number.ToString();
                            Response.Redirect("~/Forms/testRadcomboBox.aspx");
                        }
                        else
                            e.Authenticated = false;
            
                    }
            Last edited by Frinavale; Oct 12 '11, 12:55 PM. Reason: Added code tags. Please post code in code tags.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              The ASP.NET Login control works with Forms Authentication.

              The code that you posted in post #6 does not use this type of authentication. So, I don't think you can use the login control.

              You'll have to check session to see if your user is logged in and manually display the login or logout buttons/links accordingly (by setting their Visible property).

              -Frinny

              Comment

              • samaira
                New Member
                • Oct 2011
                • 7

                #8
                Hi could u plz help with how to proceed further.I was in a different domain.Recently (1.5 months bak) shifted to .net

                Comment

                • samaira
                  New Member
                  • Oct 2011
                  • 7

                  #9
                  I have also tried another way to create login using table control.in masterpage.cs my code is:
                  Code:
                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Web;
                  using System.Web.UI;
                  using System.Web.UI.WebControls;
                  
                  public partial class MasterPage : System.Web.UI.MasterPage
                  {
                      protected void Page_Load(object sender, EventArgs e)
                      {
                          if (Session["Empname"] != null)
                          {
                              string ssn = Session["EmpName"].ToString();
                              Label1.Text = ssn;
                              btnlogout.Text = "Logout";
                          }
                          else
                          {
                              btnlogout.Text = "Login";
                          }
                  
                      }
                      protected void btnlogout_Click(object sender, EventArgs e)
                      {
                          if (Session["EmpName"] == null)
                          {
                              Response.Redirect("~/login.aspx");
                          }
                          else
                          {            Response.Redirect("~/radcmbox.aspx");
                          }
                              }
                  }
                  Last edited by Frinavale; Oct 14 '11, 03:08 PM. Reason: Added code tags. Please post code in code tags.

                  Comment

                  • samaira
                    New Member
                    • Oct 2011
                    • 7

                    #10
                    In the above code if i login it wl take me to "radcmbox.a spx" & once i login and then on clicking logout how do i go to login page again?

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      It would probably be a better idea to have 2 buttons/linkbuttons instead of using 1 for two different functions.

                      Instead of changing the text of the one button, set the Visible property of each button.

                      That way you know when the user clicked the "logout" button/linkbutton and when the user clicked the "login" button/linkbutton so that you can take the appropriate action.

                      -Frinny

                      Comment

                      Working...