Click here to Skip to main content
15,887,464 members
Home / Discussions / C#
   

C#

 
QuestionImage processing Pin
IJAJ MULANI26-Jan-14 20:01
IJAJ MULANI26-Jan-14 20:01 
QuestionRe: Image processing Pin
Richard MacCutchan26-Jan-14 21:50
mveRichard MacCutchan26-Jan-14 21:50 
AnswerRe: Image processing Pin
Chris Quinn26-Jan-14 22:24
Chris Quinn26-Jan-14 22:24 
AnswerRe: Image processing Pin
BillWoodruff27-Jan-14 0:55
professionalBillWoodruff27-Jan-14 0:55 
GeneralRe: Image processing Pin
OriginalGriff27-Jan-14 2:32
mveOriginalGriff27-Jan-14 2:32 
GeneralRe: Image processing Pin
Shameel27-Jan-14 2:47
professionalShameel27-Jan-14 2:47 
AnswerRe: Image processing Pin
Rahul VB28-Jan-14 7:47
professionalRahul VB28-Jan-14 7:47 
QuestionDerek Slager's BCrypt Class for C# Check Password Method failed Pin
ahmed_one26-Jan-14 19:03
ahmed_one26-Jan-14 19:03 
Hi to all,

This is with continuation of my previous question here.

I am using Microsoft Visual Studio 2010 Ultimate, C# Winforms & MySql Database.

I am trying to use BCrypt class by Derek Slager for creating and authenticating username and password. A form is use to create new user to save Username and Password to database, but this Password is not the plaintext one, it is hashed using BCrypt.HashPassword method. I am using binding source for the form, so I am using a dummy textbox which will hold the hashed value of plaintext user entered in password textbox. Then I bind the controls using following:

C#
public void BindControls()
    {
        txtUser.Text = null;
        txtPass.Text = null;
        txtHash.Text = null;


        txtID.DataBindings.Clear();
        txtUser.DataBindings.Clear();
        txtHash.DataBindings.Clear();

        txtID.DataBindings.Add(new Binding("Text", bs, "userID"));
        txtUser.DataBindings.Add(new Binding("Text", bs, "userName"));
        txtHash.DataBindings.Add(new Binding("Text", bs, "userPass"));

    }


As plaintext password is not required to save in Database, there is no need to bind it. txtHash will get hashed password after user enter plaintext password in txtPass textbox on LeaveEvent as follows:

C#
private void txtPass_Leave(object sender, EventArgs e)
    {
      txtHash.Text = null;         
      txtHash.Text = BCrypt.HashPassword(txtPass.Text.Trim().ToString(),BCrypt.GenerateSalt(12));
    }


Then called the save method of User class when user click Save Button:

C#
private void btnSave_Click(object sender, EventArgs e)
    {

        this.Validate();
        bs.EndEdit();

        if (!ds.HasChanges())
        {

            MessageBox.Show("No changes to save.", "Saving Records");
            return;
        }


        try
        {


            fUser.SaveUser(ds);
            MessageBox.Show("Records saved.", "Saving Records");
        }


        catch (Exception exceptionObj)
        {

            MessageBox.Show(exceptionObj.Message.ToString());

        }
    }


User class save method is follows:

C#
public void SaveUser(DataSet oDs)
   {
       oCn = da.GetConnection();
       oTrn = oCn.BeginTransaction();
       sqlDataMaster = new MySqlDataAdapter();
       try
       {

           if (oCn == null)
           {
               oCn.Open();
           }


           sInsProcName = "prInsert_User";
           insertcommand = new MySqlCommand(sInsProcName, oCn, oTrn);
           insertcommand.CommandType = CommandType.StoredProcedure;
           insertcommand.Parameters.Add(new MySqlParameter("nNewID", MySqlDbType.Int32, 0, "userID"));
           insertcommand.Parameters["nNewID"].Direction = ParameterDirection.Output;
           insertcommand.UpdatedRowSource = UpdateRowSource.OutputParameters;
           insertcommand.Parameters.Add("mUserName", MySqlDbType.VarChar, 50, "userName");
           insertcommand.Parameters.Add("mUserPass", MySqlDbType.VarChar, 60, "userPass");
           sqlDataMaster.InsertCommand = insertcommand;


           sUpdProcName = "prUpdate_User";
           updatecommand = new MySqlCommand(sUpdProcName, oCn, oTrn);
           updatecommand.CommandType = CommandType.StoredProcedure;
           updatecommand.Parameters.Add("nNewID", MySqlDbType.Int32, 0, "userID");
           updatecommand.Parameters.Add("mUserName", MySqlDbType.VarChar, 50, "userName");
           updatecommand.Parameters.Add("mUserPass", MySqlDbType.VarChar, 60, "userPass");
           sqlDataMaster.UpdateCommand = updatecommand;

           sqlDataMaster.Update(oDs.Tables[0]);

           oTrn.Commit();


       }
       catch (MySqlException e)
       {
                           oTrn.Rollback();
           MessageBox.Show(e.ToString());

       }
   }


Data is saved successfully, no problem is there. In the password field of Table it is shows the hashed value. But problem arises when I need to validate the password via Login form. User enter username and password and click login, which fires following code:

C#
private void btnCheck_Click(object sender, EventArgs e)
    {
        string hashpassdb = dm.GetData("Select userPass from userMaster where userName = " + "'" + txtUser.Text.ToString() + "'" ).Rows[0]["userPass"].ToString();
        if (!BCrypt.CheckPassword(txtPass.Text.Trim().ToString(), hashpassdb))
        {
            MessageBox.Show(hashpassdb);
            MessageBox.Show("Invalid Username or Password.");
            MessageBox.Show(BCrypt.HashPassword(txtPass.Text.Trim().ToString(), hashpassdb));

        }
        else
        {
            MessageBox.Show("Login Successfull");
            ((frmMain)this.MdiParent).EnableMenu();
            this.Close();

        }
    }

    private bool DoesPasswordMatch(string userEnteredPassword, string hashedPwdFromDatabase)
    {
        return BCrypt.CheckPassword(userEnteredPassword, hashedPwdFromDatabase);

    }


dm is the instance of DAL class which contains GetData method, accepts Sql command as parameter and returns DataTable. The problem is hashed values never matched with the one using in validation, as notice in code I've use MessageBox.Show method just to ensure what value is return from Database hashed password and the one return by BCrypt.Hashpassword. They both are different. Also, I am using VarChar(60) datatype for holding hash value in Mysql.

I need some guidance about what I am doing wrong here? any help is much appreciated.

Thanks

Ahmed
AnswerRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
Garth J Lancaster26-Jan-14 19:48
professionalGarth J Lancaster26-Jan-14 19:48 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
ahmed_one26-Jan-14 20:10
ahmed_one26-Jan-14 20:10 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
Garth J Lancaster26-Jan-14 20:29
professionalGarth J Lancaster26-Jan-14 20:29 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
ahmed_one26-Jan-14 20:42
ahmed_one26-Jan-14 20:42 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
Garth J Lancaster26-Jan-14 20:49
professionalGarth J Lancaster26-Jan-14 20:49 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
ahmed_one26-Jan-14 20:59
ahmed_one26-Jan-14 20:59 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
Garth J Lancaster26-Jan-14 21:06
professionalGarth J Lancaster26-Jan-14 21:06 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
ahmed_one26-Jan-14 21:16
ahmed_one26-Jan-14 21:16 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
Garth J Lancaster26-Jan-14 23:30
professionalGarth J Lancaster26-Jan-14 23:30 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
ahmed_one26-Jan-14 23:35
ahmed_one26-Jan-14 23:35 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
Garth J Lancaster26-Jan-14 23:40
professionalGarth J Lancaster26-Jan-14 23:40 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
ahmed_one27-Jan-14 0:00
ahmed_one27-Jan-14 0:00 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
Garth J Lancaster26-Jan-14 23:44
professionalGarth J Lancaster26-Jan-14 23:44 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
ahmed_one27-Jan-14 0:02
ahmed_one27-Jan-14 0:02 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
ahmed_one28-Jan-14 3:37
ahmed_one28-Jan-14 3:37 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
Pete O'Hanlon28-Jan-14 4:00
mvePete O'Hanlon28-Jan-14 4:00 
GeneralRe: Derek Slager's BCrypt Class for C# Check Password Method failed Pin
ahmed_one28-Jan-14 5:22
ahmed_one28-Jan-14 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.