You must Sign In to post a response.
  • Category: .NET

    how check Username and Password in C#

    hi,
    I am new in C#. I have written some coding in C# to check user name and password. But my coding is checking only first record not second second. so how can my coding will check entire data for username is having or how it will check next record for username having or not.
    plese suggest.
    coding...
    OleDbDataReader myReader = cmd.ExecuteReader();

    do
    {
    while (myReader.Read())
    {
    if ((TxtUserName.Text.Trim() == myReader.GetString(0).Trim()) && (TxtPassword.Text.Trim() == myReader.GetString(1).Trim()))

    {
    MessageBox.Show ("Your Login was Succuesfully");
    Close();
    }

    else
    {
    MessageBox.Show("Your Login was Unsuccuesfully");
    }

    }

    }
    while (myReader.NextResult());
    myReader.Close();
    StrConn.Close();
  • #332336
    You could try the login control which is given in my signature
    Thanks & Regards,
    Danasegarane Arunachalam

    http://www.techreceipe.tk/

  • #332339
    dr = comm.ExecuteReader();
    if (dr.HasRows)
    {
    while (dr.Read())
    {
    if (TxtUserName.Text.Trim() == dr[0].ToString() && TxtPassword.Text.Trim() == dr[0].ToString())
    {
    Response.Write("You are successfully loggedin");
    break;
    }
    }
    }

  • #332358
    Hi,

    private void btnLogin_Click(object sender, EventArgs e)
    {
    MyConnection.Open();

    try
    {
    str = "SELECT fullname,name FROM tbluser where login='" + txtLoginId.Text + "' and password=MD5('" + txtPassword.Text + "')";
    cmd = new OdbcCommand(str, MyConnection);
    rd = cmd.ExecuteReader();
    if (rd.Read())
    {
    Program.unm = rd.GetString(1);
    frmItemList m = new frmItemList();

    m.Show();
    this.Hide();
    }
    else
    {
    MessageBox.Show("User Name or Password is incorrect");
    txtLoginId.Text = "";
    txtPassword.Text = "";
    this.Show();
    }
    }
    catch
    {
    }
    MyConnection.Close();
    }



    Thanks & Regards,
    Deepika - Sr. Editor
    Webmaster, GujaratSpider
    http://angeldeeps.blogspot.com/
    "Experience is the name everyone gives to his mistakes"

  • #332581

    protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
    {
    clsUserMst objUserMst = new clsUserMst();

    DbDataReader objdr = null;
    int iUserId = 0;
    string strUserNm = "";
    objdr= objUserMst.Login(txtUserNm.Text.ToString().Trim(),objUserMst.Encryption((txtPassword.Text.ToString().Trim())));
    if (objdr.HasRows)
    {
    while (objdr.Read())
    {
    string jv = "<script>alert('Welcome Home!!!');</script>";
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", jv, false);
    }
    }
    else
    {
    string jv = "<script>alert('Invalid User or Wrong Password!!!');</script>";
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", jv, false);
    }
    objdr.Close();


    }



    Use the above code use dbreader to compare username and password to get the valid user login

    Thanks & Regards,
    Anand Babu S

  • #333189
    int ret = validateusers(txtemail.Text, txtpassword.Text);
    if (ret == 1)
    {
    objGet._tableName = "tbl_Users";
    objGet._ColumnName = " user_password ";
    objGet._where = " user_userName='" + txtemail.Text + "' and user_password='" + txtpassword.Text + "'";
    DataTable dtblGetuser = new DataTable();
    dtblGetuser = objGet.GetCustomValuesByID();
    if (dtblGetuser.Rows.Count > 0)
    {
    if (txtpassword.Text == dtblGetuser.Rows[0]["user_password"].ToString())
    {
    lblMsg.Visible = false;

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
    1, // Ticket version
    txtemail.Text, // Username associated with ticket
    DateTime.Now, // Date/time issued
    DateTime.Now.AddMinutes(30), // Date/time to expire
    true, // "true" for a persistent user cookie
    objUser.uspGetUserRolebyEml(txtemail.Text), // User-data, in this case the roles
    FormsAuthentication.FormsCookiePath);// Path cookie valid for

    // Encrypt the cookie using the machine key for secure transport
    string hash = FormsAuthentication.Encrypt(ticket);
    HttpCookie cookie = new HttpCookie(
    FormsAuthentication.FormsCookieName, // Name of auth cookie
    hash); // Hashed ticket

    // Set the cookie's expiration time to the tickets expiration time
    if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

    // Add the cookie to the list for outgoing response
    Response.Cookies.Add(cookie);


    HttpCookie ck = new HttpCookie("userDet_Islamic", objUser.uspGetUserbyEml(txtemail.Text));
    ck.Expires = DateTime.Now.AddMinutes(30);
    Response.Cookies.Add(ck);
    String[] struserName = Request.Cookies["userDet_Islamic"].Value.ToString().Split(',');

    if (struserName.Length > 2)
    {

    uid = Convert.ToInt64(struserName[1].ToString());
    string strrole = struserName[0].ToString();


    string strReturnurl = "";
    if (Request["ReturnUrl"] != null)
    {
    strReturnurl = Request["ReturnUrl"].ToString();
    }
    else
    {
    if (strrole == "admin")
    {
    strReturnurl = "~/admin/default.aspx";
    }
    else
    {
    strReturnurl = "~/user/default.aspx";
    }
    }

    Response.Redirect(strReturnurl);
    }
    else
    {
    struserName = objUser.uspGetUserbyEml(txtemail.Text).Split(',');
    if (struserName.Length > 2)
    {

    uid = Convert.ToInt64(struserName[1].ToString());
    string strrole = struserName[0].ToString();


    string strReturnurl = "";
    if (Request["ReturnUrl"] != null)
    {
    strReturnurl = Request["ReturnUrl"].ToString();
    }
    else
    {
    if (strrole == "admin")
    {
    strReturnurl = "~/admin/default.aspx";
    }
    else
    {
    strReturnurl = "~/user/myProperty.aspx";
    }
    }

    Response.Redirect(strReturnurl);
    }
    }

    }
    else
    {
    lblMsg.Visible = true;
    lblMsg.Text = "Username/password not matched.";
    }
    }
    else
    {
    lblMsg.Visible = true;
    lblMsg.Text = "Username/password not matched.";
    }

    }
    else if (ret == 2)
    {
    lblMsg.Visible = true;
    lblMsg.Text = "Deactivated by Administrator.";
    }
    else if (ret == 3)
    {
    lblMsg.Visible = true;
    lblMsg.Text = "Account not Confirmed.";
    }
    else
    {
    lblMsg.Visible = true;
    lblMsg.Text = "Username/password not matched.";
    }
    }


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.