6

I try to make custom login in my web app

Here is 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 make controller like this :

 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 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 Email and Password I have this error:

`An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code

Additional information: Sequence contains no elements`

In this row

 var usr = db.userAccount.Single(u => u.Email == user.Email && u.Password == user.Password);

How can I fix it?

anon
  • 855
  • 12
  • 22
  • 1
    Not exactly related to your problem now, but I would advise you not to use that login system in production. You are clearly storing passwords in plain text, which means they will all be compromised if someone gets in the database. Typically you shouldn't roll your own login system, ASP.NET Identity works quite nicely and handles secure storage for you. – juunas Mar 03 '17 at 09:07

0 Answers0