title: creating a login controller in MVC project
body: <p>So I am creating a web application with ASP.NET MVC4 in framework 4.5. I wish to add an account system. I have created a database with users and roles and now need to connect that with my web app.</p>
  
  <p>I have used the visual studio tool for adding ADO.NET Entity Data Model, and have connected to my database, selected all the tables and clicked generate. I got classes for the user and group tables, as well as a class for <code>database.context</code>(not sure what this is). After the wizard created the class files, I went out to find a way to protect the users (password encryption and user validation) and came across <a href="https://crackstation.net/hashing-security.htm?=rd#aspsourcecode" rel="nofollow">this</a> web article. I created the class inside the models folder.</p>
  
  <p>Now its on to the controller, and here lies the problem. When i look at the source for the template project that has web login integrated into the template i only get confused.  the methods in the login controler always have the <code>returnurl</code> parameter and i have no idea where that comes from. Also it uses the <code>WebSecurity.Login()</code> method to login and i am wondering how to make my web app use the methods that i have referenced before.</p>
  
  <p>Ofc, if  there is a simpler way to do this without letting VS auto generate a lot of code that i don't know what to do with, it is preferable to use that instead, I don't want to have code that's not useful to the app if i don't need to.</p>
  
  <p>The database itself is custom, that is to say the tables that are autogenerated by the internet application template are not suitable for my needs. most of the tutorials i found on the web, includint the one in the comments is working with the build in entity framework database with the userProfile table and membership table. Is there maybe a way i can adjust this, so  that the tables created have a different structure? If that approach is better ofc.</p>
  
  <p>UPDATE:
  So i got to this point in the code. i have created a users controller with the following methods:</p>
  
  <pre><code>internal bool UserExists(LoginModel model)
  private void CheckIfFirstDeploy()
  private void RegisterFirstAdmin()
  private void registerUser(string ALPHA_ADMIN_USERNAME, string ALPHA_ADMIN_PASSWORD, Group group)
  internal bool PasswordValid(LoginModel model)
  </code></pre>
  
  <p>the account controller (that is another class and file), instances the users controller and runs like for authentication:</p>
  
  <pre><code>[HttpPost]
  [AllowAnonymous]
  public ActionResult Login(LoginModel model, string returnUrl)
  {
      UsersController uc = new UsersController();
      if (ModelState.IsValid &amp;&amp; uc.UserExists(model) &amp;&amp; uc.PasswordValid(model))
      {
          FormsAuthentication.SetAuthCookie(model.UserName, false);
          return View("Dashboard");
      }
      // If we got this far, something failed, redisplay form
      ModelState.AddModelError("", "The user name or password provided is incorrect.");
      return View(model);
  }
  </code></pre>
  
  <p>When the app notices that there's no entries for users it runs the <code>CheckIfFirstDeploy()</code> function, fills the groups table with the first 3 roles and creates the first admin user as presented below:</p>
  
  <pre><code>private void RegisterFirstAdmin(){
      const string ALPHA_ADMIN_USERNAME = "username";
      const string ALPHA_ADMIN_PASSWORD = "password";
  
      if (db.Groups.Count() == 0)
      {
          db.Groups.Add(new Groups()
          {
              GroupName = "Administrator"
          });
          db.Groups.Add(new Groups()
          {
              GroupName = "Manager"
          });
          db.Groups.Add(new Groups()
          {
              GroupName = "User"
          });
      }
  
      registerUser(ALPHA_ADMIN_USERNAME, 
                   ALPHA_ADMIN_PASSWORD, 
                   Group.Administrator);
  
  
  }
  
  private void registerUser(string ALPHA_ADMIN_USERNAME, 
                            string ALPHA_ADMIN_PASSWORD, 
                            Group group) {
  
      db.Users
          .Add(
              new Users() { 
                  Email = ALPHA_ADMIN_USERNAME,
                  Hash = Crypto.PasswordHash.CreateHash(ALPHA_ADMIN_PASSWORD),
                  GroupId = 1
              }
          );
  }
  </code></pre>
  
  <p>When i run the app i come into a few problems. First, it seems that the database doesn't get filled with the groups that i have intended to insert, and second at the point where i have 
  <code>if (db.Users.Count(u =&gt; u.Email == model.UserName) != 1)</code></p>
  
  <p>I get the following error:</p>
  
  <p><code>The data types text and varchar are incompatible in the equal to operator.</code></p>
  
  <p>The types in the database are text and in the code its defined as string.</p>