I am trying to make a user login in my web application
Here is the model:
public class Clients
{
public int ID { get; set; }
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
[Display(Name = "..")]
public string UserName { get; set; }
[Display(Name = "")]
public string Position { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "")]
public string Password { get; set; }
[Compare("Password", ErrorMessage = " .")]
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
}
I am making a controller as follows:
public class ClientsLoginController : Controller
{
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(Clients user )
{
using (OurDbContext db = new OurDbContext())
{
var usr = db.userAccount.Single(u => u.Email == user.Email && u.Password == user.Password);
if (usr != null)
{
Session["UserId"] = usr.ID.ToString();
Session["Email"] = usr.Email.ToString();
return RedirectToAction("Login");
}
else
{
ModelState.AddModelError("","Email or Login not correct");
}
return View();
}
}
}
Here is a view:
<h2>Login</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Clients</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
When I fill in the email address and password, I have this error:
A 'System.InvalidOperationException' type exception occurred in System.Core.dll but was not processed in user code
Additional Information: The sequence does not contain `
In this line
var usr = db.userAccount.Single(u => u.Email == user.Email && u.Password == user.Password);
How can i fix this?
user7629010
source
share