Login cannot redirect to another page

Donald Symmons 2,996 Reputation points
2023-01-13T16:06:10.11+00:00

I recently tried to use authentication and authorization on my login, but when I click to login nothing happens; it does not redirect

here is my code

Login.aspx.cs

 private bool ValidateUser(string email, string pass)
        {
            SqlConnection conn;
            SqlCommand cmd;
            string lookupPassword = null;

            // Check for invalid userName.
            // userName must not be null and must be between 1 and 15 characters.
            if ((null == email) || (0 == email.Length) || (email.Length > 15))
            {
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.");
                return false;
            }

            // Check for invalid passWord.
            // passWord must not be null and must be between 1 and 25 characters.
            if ((null == pass) || (0 == pass.Length) || (pass.Length > 25))
            {
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.");
                return false;
            }

            try
            {
                // Consult with your SQL Server administrator for an appropriate connection
                // string to use to connect to your local SQL Server.
                conn = new SqlConnection("Data Source=(LocalDB)
                conn.Open();

                // Create SqlCommand to select pwd field from users table given supplied userName.
                cmd = new SqlCommand("Select pass from Users where email=@email", conn);
                cmd.Parameters.Add("@email", SqlDbType.NVarChar, 25);
                cmd.Parameters["@email"].Value = email;

                // Execute command and fetch pwd field into lookupPassword string.
                lookupPassword = (string)cmd.ExecuteScalar();

                // Cleanup command and connection objects.
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                // Add error handling here for debugging.
                // This error message should not be sent back to the caller.
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message);
            }

            // If no password found, return false.
            if (null == lookupPassword)
            {
                // You could write failed login attempts here to event log for additional security.
                return false;
            }

            // Compare lookupPassword and input passWord, using a case-sensitive comparison.
            return (0 == string.Compare(lookupPassword, pass, false));
        }

        private void CmdLogin_ServerClick(object sender, System.EventArgs e)
        {
            if (ValidateUser(txtUserName.Value, (txtUserPass.Value)))
                FormsAuthentication.RedirectFromLoginPage(txtUserName.Value, chkPersistCookie.Checked);
            else
                Response.Redirect("Login.aspx", true);
        }

Login.aspx


                    
                    
                    
                       <asp:Label ID="lblMsg" ForeColor="red" Font-Size="10" runat="server" />
                    
                    <label for="txtUsername" style="font-weight: 500;">Email</label>
                    <input id="txtUserName" type="text" runat="server" class="form-control" style="font-size: 11pt;" placeholder="Email Address"/>
                     <asp:RequiredFieldValidator ControlToValidate="txtUserName" Display="Static" ErrorMessage="Field Required" ForeColor="Red" Font-Size="9pt" runat="server" ID="vUserName" />
                    
                    <label for="txtPassword" style="font-weight: 500;">Password</label>
                    <input id="txtUserPass" type="password" runat="server" class="form-control" style="font-size: 11pt;" placeholder="Password"/>
                    <asp:RequiredFieldValidator ControlToValidate="txtUserPass" Display="Static" ErrorMessage="Field Required" ForeColor="Red" Font-Size="9pt" runat="server" ID="vUserPass" />
                    
                    
                    Remember me: <asp:CheckBox ID="chkPersistCookie" runat="server" AutoPostBack="false" />
                    
                    <input type="submit" value="Login" runat="server" class="btn btn-primary" id="CmdLogin" style="background-color: #32657c;" />
                
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Developer technologies | ASP.NET Core | Other

3 answers

Sort by: Oldest
  1. Jose Zero 576 Reputation points
    2023-01-14T12:32:18.7066667+00:00

    The RedirectFromLoginPage method redirects to the return URL specified in the query string using the ReturnURL variable name. If the ReturnURL variable does not exist, the RedirectFromLoginPage method redirects to the URL in the DefaultUrl property.

    Sounds you missed set DefaultUrl in web.config.
    As an example:

    <authentication mode="Forms">
      <forms loginUrl="member_login.aspx"
        defaultUrl="index.aspx" />
    </authentication>
    

    Was this answer helpful?


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    3 deleted comments

    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    8 deleted comments

    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.