Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

Can anyone know?

in login page i have 2 fields 1)loginemailid 2)password....in this


when i enter both field by no id,password not in DB it should display "Please Register to login" and when i enter the password alone weong it should display" "Invalid username and password"..................
Posted

Sample code :

C#
private void CheckPassword()
       {
           string emailAddress = this.txtLoginRmailId.Text;
           string password = this.txtPassword.Text;
           string errorMessage;

           bool userExist = Bll.CheckIfUserExist(emailAddress);
           if (userExist == false)
           {
               errorMessage = "Please register to login";

               Response.Write(errorMessage);
           }
           else
           {
               bool userIsValid = Bll.ValidateUserCredential(emailAddress, password);
               if (userIsValid == false)
               {
                   errorMessage = "Invalid username and password";
               }

               Response.Write(errorMessage);
           }
       }
 
Share this answer
 
if you are using login control..

XML
<asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
<asp:Login ID="ctlLogin" runat="server" OnAuthenticate="OnAuthenticate">
<LayoutTemplate>
    <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
    <asp:TextBox ID="Password"  runat="server" TextMode="Password" ></asp:TextBox>
    <asp:Button ID="Button2" ValidationGroup="ctlLogin" CommandName="Login" runat="server" Text="Button" />
</LayoutTemplate>
</asp:Login>



code behind...


C#
bool Authenticated;
    protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
    {
        Authenticated = false;
        Authenticated = UserAuthenticate(ctlLogin.UserName, ctlLogin.Password);
        e.Authenticated = Authenticated;
        if (Authenticated == true)
        {
            Response.Redirect("~/home.aspx");
        }
    }
    
    private bool UserAuthenticate(string UserName, string Password)
    {
        bool passwordMatch = false;
        SqlCommand cmd = new SqlCommand("Select email,Password FROM [User] where Email=@userName", con);
        cmd.CommandType = CommandType.Text;
        //Usage of Sql parameters also helps avoid SQL Injection attacks.
        SqlParameter sqlParam = cmd.Parameters.Add("@userName", SqlDbType.VarChar, 100);
        sqlParam.Value = UserName;
        try
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read(); // Advance to the one and only row
            // Return output parameters from returned data stream
            if (reader.HasRows) // check that reader contain the record or not
            {
                if (reader != null)
                {
                    string dbid = reader.GetString(0);
                    string dbpass = reader.GetString(1);
                    if (dbid == UserName && dbpass == Password)
                    {
                        passwordMatch = true;
                    }
                }
            }
            else
            {
                lblMessage.Text = "You have not registered yet!";
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Execption verifying password. " + ex.Message);
            //lblMessage.Text = "Login error: " + ex.Message;
        }
        finally
        {
            con.Close();
        }
        return passwordMatch;
    }



hope you will get idea. good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900