When I set the VisibleWhenLoggedIn property of my Login control to false, my Login control automatically becomes invisible even if I'm not logged in. I've already set the Authenticate event to use my own SQL database, but this problem is still persisting. How do I fix this?

Here is the code:

<asp:Login ID="frmLogin" runat="server" onauthenticate="frmLogin_Authenticate" VisibleWhenLoggedIn="False"></asp:Login>
protected void frmLogin_Authenticate(object sender, AuthenticateEventArgs e)
    {
        string email = frmLogin.UserName;
        string pw = frmLogin.Password;

        string connection = "Data Source=[Removed for privacy purposes];
        SqlConnection conn = new SqlConnection(connection);
        conn.Open();

        string command = "SELECT [EmailAddress], [Password] FROM Members WHERE (EmailAddress = '" + email + "' AND Password = '" + pw + "')";
        SqlCommand cmd = new SqlCommand(command, conn);
        cmd.ExecuteNonQuery();

        SqlDataReader myReader = cmd.ExecuteReader();
        bool hasContent = myReader.Read();

        if (hasContent == true)
        {
            e.Authenticated = true;
            Response.Redirect("/Default.aspx");
        }
        else
        {
            e.Authenticated = false;
            frmLogin.FailureText = "Incorrect email/password combination.";
        }  
    }

did u get any errors or login control wont show up ?

Hi

You can refer following code for login control and connection.

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
          bool Authenticated = false;
          Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
          e.Authenticated = Authenticated;
          if (Authenticated == true)
          {
                    Response.Redirect("Home.aspx");
          }
}
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
          bool boolReturnValue = false;
          // Insert code that implements a site-specific custom 
          // authentication method here.
          // This example implementation always returns false.
          string strConnection  = "server=dtpxp-skumari;database=master;uid=sa;pwd=;";
          SqlConnection Connection = new SqlConnection(strConnection);
          String strSQL = "Select * From Employee";
          SqlCommand command =new SqlCommand(strSQL, Connection);
          SqlDataReader Dr;
          Connection.Open();
          Dr=command.ExecuteReader();
          while (Dr.Read())
          { 
                    if ((UserName == Dr["name"].ToString()) & (Password == Dr["Password"].ToString()))
                    {
                             boolReturnValue = true;
                    } 
                    Dr.Close();
                    return boolReturnValue;
          }
}

If you want to more detail about login control just go for this below link.

http://www.c-sharpcorner.com/uploadfile/sushmita_kumari/logincontrol101312006002845am/logincontrol1.aspx?articleid=c33d0072-8f7c-4958-a7dc-ca1809737193

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.