Hello folk, first I would like to tell you that I am new to CSharp and that is why I did not know that my way of creating the connection with MySQL was wrong, so because of that I had to change it with Prepared Statement. Please be kind!
THis is the method that I am trying to make it works.
[HttpPost]
public ActionResult ValidatesLogin (User eUser)
{
var Email = eUser.Email;
var Password = eUser.Password;
try
{
MySqlCommand cmd = MySqlConn.cmd;
cmd = new MySqlCommand(" from User " + "WHERE Email=@email " + "AND Password=@password",
MySqlConn.conn);
cmd.Prepare();
cmd.Parameters.AddWithValue("@email", username);
cmd.Parameters.AddWithValue("@password", password);
int result = (int)cmd.ExecuteReader();
//encrypting the login user password
//the encrypter and decrypter must have same key which is 'kahat' below
var encryptpassword = EncryptHelper.EncryptString(eUser.Password, "kahat");
// Returns true when username and password match:
if (result > 0)
{
System.Web.Security.FormsAuthentication.SetAuthCookie(eUser.Email, false);
return RedirectToAction("Index", "Index", new { area = "Index" });
}
else
{
TempData["Message"] = "Login failed. Email or password supplied doesn't exist.";
return View("Index");
}
}
catch (Exception ex)
{
return ThrowJsonError(ex);
}
}
And this code below is my actual connection which works very well.
[HttpPost]
public ActionResult Validate(User eUser)
{
try
{
//Pull email and password from login page
var Email = eUser.Email;
var Password = eUser.Password;
//Pull first email and password from database
var currentUser = this.rpGeneric.Find<User>(" from User WHERE Email=:email ", new string[] { "email" }, new object[] { Email }).FirstOrDefault();
//encrypting the login user password
//the encrypter and decrypter must have same key which is 'kahat' below
var encryptpassword = EncryptHelper.EncryptString(eUser.Password, "kahat");
//Comparing user password in login page and current password in db
if (currentUser != null && encryptpassword.Equals(currentUser.Password, StringComparison.Ordinal) && currentUser.EmailConfirmed == true)
{
System.Web.Security.FormsAuthentication.SetAuthCookie(eUser.Email, false);
return RedirectToAction("Index", "Index", new { area = "Index" });
}
else
{
TempData["Message"] = "Login failed. Email or password supplied doesn't exist.";
return View("Index");
}
}
catch (Exception ex)
{
return ThrowJsonError(ex);
}
}