I've built a basic login page that accepts a username and a password, I've added a login method to my controller but I can figure out how to pass the username and password from the view to my controller, The userName and userPassword parameters are always null. Any help here will be greatly appreciated.
public async Task<IActionResult> Login(string userName, string userPassword)
{
if (ModelState.IsValid)
{
var user = users.Where(x => x.UserName == userName && x.UserPassword == userPassword).FirstOrDefault();
if (user == null)
{
ViewBag.Message = "Invalid Credential";
return View(users);
}
else
{
var claims = new List<Claim>() {
new Claim(ClaimTypes.NameIdentifier, Convert.ToString(user.UserId)),
new Claim(ClaimTypes.Name, user.UserName),
new Claim("FavoriteDrink", "Tea")
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties()
{
IsPersistent = user.RememberLogin
});
return LocalRedirect(user.ReturnUrl);
}
}
return View(users);
}
<div class="login-box">
<h2>Login</h2>
<form id="AccountLogin" method="post" asp-controller="Accounts" asp-action="Login">
<div asp-validation-summary="All" class="text-danger">
<div class="user-box">
<input id="uName" asp-for="UserName" type="text" name="" required="">
<label>Username</label>
<span asp-validation-for="UserName" class="text-danger"></span>
</div>
<div class="user-box">
<input id="pWord" asp-for="UserPassword" type="password" name="" required="">
<label>Password</label>
<span asp-validation-for="UserPassword" class="text-danger"></span>
</div>
<a asp-controller="Accounts" asp-action="Login">
<span></span>
<span></span>
<span></span>
<span></span>
<input type="submit" value="Login" class="btn text-light"
style="opacity: 1; border:0; font-size:16px; text-transform:uppercase; letter-spacing: 2px"/>
</a>
</div>
</form>
</div>
Many thanks