I am using FirstorDefault in my linq query. I still get the above error.
My model
public partial class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[Required]
[StringLength(20)]
public string UserName { get; set; }
[Required]
[StringLength(50)]
public string UserPassword { get; set; }
public DateTime? LastModified { get; set; }
public bool? Inactive { get; set; }
[StringLength(50)]
public string Firstname { get; set; }
[StringLength(50)]
public string Lastname { get; set; }
[StringLength(30)]
public string Title { get; set; }
[StringLength(3)]
public string Initial { get; set; }
[StringLength(100)]
public string EMail { get; set; }
public DateTime? UserLastLogin { get; set; }
public int? FailedLoginAttempts { get; set; }
[StringLength(1)]
public string PasswordChangeatLogin { get; set; }
public bool? IsAdmin { get; set; }
}
}
My code in the action
public ActionResult Login(Models.User user)
{
// var errors = ModelState
//.Where(x => x.Value.Errors.Count > 0)
//.Select(x => new { x.Key, x.Value.Errors })
//.ToArray();
if (ModelState.IsValid)
{
if (IsValid(user.UserName, user.UserPassword))
{
FormsAuthentication.SetAuthCookie(user.UserName, true);
return RedirectToAction("Default", "Home");
}
else
{
ModelState.AddModelError("", "Login data is incorrect!");
}
}
return View(user);
}
private bool IsValid(string username, string password)
{
bool IsValid = false;
using (var db = new PowerERP.Models.PowerModel())
{
var user = db.Users.FirstOrDefault(u => u.UserName == username);
if (user != null)
{
var encryptedPassword = CryptorEngine.Encrypt(password.Trim(), true);
if (user.UserPassword == encryptedPassword)
{
IsValid = true;
}
}
}
return IsValid;
}
Can somebody help?? PLEASE stuggling for the last 1 day. Let me know if your require any more info
Thanks
Rajesh Moorthy