title: ASP.Net Core MVC Login user
body: <p>I have an ASP.Net Core MVC project I am working on, and I simply would like my user to login, and once logged in be brought to the userhome page.</p>
  
  <p>I have created the model, the controller, and the view. In the View, I am not sure how to pass the full user into my controller because I have a [Bind] User in my method. To pass in a individual property I would use Model.Id, I'm assuming I need Model for the full object? </p>
  
  <p>I am completely new with ASP.Net MVC. If you have any questions, please let me know.</p>
  
  <pre><code>// UserDataAccess login method with username or email, not sure if this is the correct way
  public string ValidateLogin(User user)
  {
      using(SqlConnection conn = new SqlConnection(connectionString))
      {
          SqlCommand cmd = new SqlCommand("spUserLogin", conn);
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("@LoginID", user.Username);
          cmd.Parameters.AddWithValue("@LoginEmail", user.Email);
          cmd.Parameters.AddWithValue("@LoginPassword", user.Password);
  
          conn.Open();
          string result = cmd.ExecuteScalar().ToString();
          conn.Close();
  
          return result;
  }
  </code></pre>
  
  <pre><code>// LoginController 
  [HttpGet]
  public IActionResult UserLogin()
  {
      return View();
  }
  
  [HttpPost]
  [ValidateAntiForgeryToken]
  public async Task&lt;IActionResult&gt; UserLogin([Bind] User user)
  {
      //ModelState.Remove("Name");// ??
  
      if(ModelState.IsValid)
      {
          string loginStatus = objUser.ValidateLogin(user);
  
          if(loginStatus == "Success")
          {
              var claims = new List&lt;Claim&gt;
              {
                  new Claim(ClaimTypes.PrimarySid, user.Id.ToString(), ClaimValueTypes.Integer),
                  new Claim(ClaimTypes.Name, user.Username, ClaimValueTypes.String),                        
                  new Claim(ClaimTypes.Email, user.Email, ClaimValueTypes.String),
                  new Claim(ClaimTypes.GivenName, user.Name, ClaimValueTypes.String)
              };
  
              //ClaimsIdentity userIdentity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
              ClaimsIdentity userIdentity = new ClaimsIdentity(claims, "login");
              ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);
  
              await HttpContext.SignInAsync(principal);
  
              return RedirectToAction("UserHome", "User");
          }
          else
          {
              TempData["UserLoginFailed"] = "Login Failed. Plz enter correct data!";
              return View();
          }
      }
      else
      {
          return View();
      }
  }
  
  [Authorize]
  public IActionResult UserHome()
  {
      return View();
  }
  
  [HttpGet]
  public async Task&lt;IActionResult&gt; Logout()
  {
      await HttpContext.SignOutAsync();
      return RedirectToAction("Login", "UserLogin");
  }
  </code></pre>
  
  <pre><code>// Login View
  @model MVC_Login.Models.User
  @*&lt;form asp-action="UserLogin"&gt;*@
  
  &lt;form method="post" action="@Url.Action("UserLogin", "Login", new { Model })"&gt;
      &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;label asp-for="Username" class="control-label"&gt;&lt;/label&gt;
          &lt;input asp-for="Username" class="form-control" /&gt;
          &lt;span asp-validation-for="Username" class="text-danger"&gt;&lt;/span&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;label asp-for="Password" class="control-label"&gt;&lt;/label&gt;
          &lt;input asp-for="Password" class="form-control" /&gt;
          &lt;span asp-validation-for="Password" class="text-danger"&gt;&lt;/span&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;input type="submit" value="Login" class="btn btn-primary" /&gt;
          @*&lt;a asp-action="Create" class="btn btn"&gt;Sign Up&lt;/a&gt;*@
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>