title: How authenticate a user using ASP.NET MVC?
body: <p>I'm creating a web app with .net using a login for users. After registration the users' data is stored in my local SQL database and the user must be able to log in using these. </p>
  
  <p>Currently I've managed to register users and they can log in when I use a hard coded password and user name but I can't figure out how I can check the user's given credentials and the ones in the database to check if these match.</p>
  
  <p>I did extensive research but didn't find a proper solution but surprisingly could not come up with a working solution.</p>
  
  <p>Here is my current code:</p>
  
  <p><strong>My users model:</strong></p>
  
  <pre><code>namespace Models
  {
      public class Users
      {
          public int Id { get; set; }
          public string userName { get; set; }
          public string userPassword { get; set; }
          public string userEmail { get; set; }
          public string userCompany { get; set; }
  
  
          public class UsersDBContext : DbContext
          {
              public DbSet&lt;Users&gt; Users { get; set; }
          }
  
  
      }
  }
  </code></pre>
  
  <p><strong>My controller</strong></p>
  
  <pre><code>namespace Eindwerk.Controllers
  {
      public class UsersController : Controller
      {
          private Users.UsersDBContext db = new Users.UsersDBContext();
  
          // GET: /Users/    
          public ActionResult Index()
          {
              return View();
          }
  
          [HttpPost]
          public ActionResult Index(Users users)
          {
              if (ModelState.IsValid)
              {
                  if(users.userName == "NAAM" &amp;&amp; users.userPassword == "WACHTWOORD")
                  {
                      FormsAuthentication.SetAuthCookie(users.userName, false);
                      return RedirectToAction("", "Home");
                  }
                  {
                      ModelState.AddModelError("", "Invalid username and/or password");
                  }
              }
  
              return View();
          }
  </code></pre>
  
  <p><strong>My View</strong></p>
  
  <pre><code>@model Eindwerk.Models.Users
  
  @{
      ViewBag.Title = "Login";
      Layout = "../_Login.cshtml";
  
  }
  @using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
  
      &lt;div class="panel-body"&gt;
      &lt;fieldset&gt;
          &lt;div class="editor-field"&gt;
              @Html.TextBoxFor(model =&gt; model.userName, new { @class = "form-control", Value="Username"})
              @Html.ValidationMessageFor(model =&gt; model.userName)
          &lt;/div&gt;
                  &lt;br /&gt;
          &lt;div class="editor-field"&gt;
              @Html.TextBoxFor(model =&gt; model.userPassword, new { @class = "form-control", Value= "Password"  })
              @Html.ValidationMessageFor(model =&gt; model.userPassword)
          &lt;/div&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;p&gt;
              &lt;input type="submit" value="SIGN IN" class="btn btn-outline btn-primary btn-lg btn-block"/&gt;
          &lt;/p&gt;
  
      &lt;/fieldset&gt;
          &lt;/div&gt;
  }
  </code></pre>
  
  <p>So instead of using  <code>if(users.userName == "NAAM" &amp;&amp; users.userPassword == "WACHTWOORD")</code>
  I want to check properly if the user is valid and registered in my database or not so I can grant or deny access. </p>
  
  <p>Anybody a proper solution? Or link to decent documentation in order to resolve this issue?</p>
  
  <p>Any help is really appreciated! </p>