title: ASP.NET MVC Identity login using database values
body: <p>For the past couple of days I've been trying to get the login functionality working for my ASP.NET MVC application. The user data for my application is stored in a MS SQL database (2008 R2). The table for this data is as follows:</p>
  
  <pre><code>table EMPLOYEE
  EMPLOYEECODE        int                  identity
  NAME                varchar(75)          not null
  PASSWORD            varchar(255)         not null
  </code></pre>
  
  <p>Which results in the following model using the Entity Framework:</p>
  
  <pre><code>public partial class EMPLOYEE
  {    
      public int EMPLOYEECODE { get; set; }
      public string NAME { get; set; }
      public string PASSWORD { get; set; }
  }
  </code></pre>
  
  <p>What I would like to do, is edit the provided (default) AccountController to enable it to use EMPLOYEE(EMPLOYEECODE, PASSWORD) to verify and login users. I think I need to alter my UserManager to make it use EMPLOYEE and the fields EMPLOYEECODE and PASSWORD but I don't know how. The current login method is as follows:</p>
  
  <pre><code>[HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
  public async Task&lt;ActionResult&gt; Login(LoginViewModel model, string returnUrl)
  {
      if (ModelState.IsValid)
      {
          var user = await UserManager.FindAsync(model.UserName, model.Password);
          if (user != null)
          {
              await SignInAsync(user, model.RememberMe);
              return RedirectToLocal(returnUrl);
          }
          else
          {
             ModelState.AddModelError("", "Invalid username or password.");
          }
      }
  
      // If we got this far, something failed, redisplay form
      return View(model);
  }
  </code></pre>
  
  <p>My UserManager is instanciated as follows:</p>
  
  <pre><code>public AccountController()
          : this(new UserManager&lt;ApplicationUser&gt;(new UserStore&lt;ApplicationUser&gt;(new ApplicationDbContext())))
      {
      }
  </code></pre>
  
  <p>How could I solve this problem? I have also tried using an authentication cookie, but I don't know how I can retrieve the username from the cookie. Any help would be highly appreciated!</p>