I’ve a UserManager Model.it’s looks like:
public class UserManager
{
private ToLetDBEntities TLE = new ToLetDBEntities();
public string GetUserPassword(string usermail)
{
var user = from o in TLE.users where o.email_add == usermail select o;
if (user.ToList().Count > 0)
{
return user.First().password;
}
else
return string.Empty;
}
}
and i’ve a AccountController like:
public class AccountController : Controller
{
[HttpPost]
public ActionResult LogOn(UserLogOn model)
{
if (ModelState.IsValid)
{
UserManager um = new UserManager();
string pass = um.GetUserPassword(model.email_add);
if (model.password==pass)
{
FormsAuthentication.SetAuthCookie(model.email_add, false);
return RedirectToAction("Welcome", "Home");
}
else
{
ModelState.AddModelError("", "The provided password is incorrect.");
}
}
return View(model);
}
}
and i’ve a UserLogon Model contains(email_add,password). When i run this controller its always gives me “The provided password is incorrect” error message. if(model.password==pass) this condition always gives false. please anyone help me.Thanks in Advance..
You need to do some simple debugging.
I.e.
UPDATE: From comments
Because you are using nchar(15) as the database column type, the string that comes from the database will be padded with extra white space.
You could fix the issue by trimming the password when you return it – like so:
But the better fix would be to use nvarchar for the database column type and then you wouldn’t have the whitespace padding problem.
BONUS: In the GetUserPassword method, you are enumerating the
userIQueryable collection twice – this will result in two DB calls.Change it to something like this to avoid that: