Home > OS >  Passing username and password to controller method asp.net mvc
Passing username and password to controller method asp.net mvc

Time:09-17

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 >
    <h2>Login</h2>
    <form id="AccountLogin" method="post" asp-controller="Accounts" asp-action="Login">
        <div asp-validation-summary="All" >
            <div >
                <input id="uName" asp-for="UserName" type="text" name="" required="">
                <label>Username</label>
                <span asp-validation-for="UserName" ></span>
            </div>
            <div >
                <input id="pWord" asp-for="UserPassword" type="password" name="" required="">
                <label>Password</label>
                <span asp-validation-for="UserPassword" ></span>
            </div>
            <a asp-controller="Accounts" asp-action="Login">            
                <span></span>
                <span></span>
                <span></span>
                <span></span>
                <input type="submit" value="Login"  
                    style="opacity: 1; border:0; font-size:16px; text-transform:uppercase; letter-spacing: 2px"/>
            </a>
        </div>
    </form>
</div>

Many thanks

CodePudding user response:

Try giving the name property of the input with the same value as the action input parameter

<input id="uName" asp-for="UserName" type="text" name="userName" required="">

CodePudding user response:

public class UsersViewModel
{
    public string UserName { get; set; }
    public string Password { get; set; }
}

after creating a view model like

public async Task<IActionResult> Login(UsersViewModel userVM)
{
    if (ModelState.IsValid)
    {
            var user = users.Where(x => x.UserName == userVM.UserName && x.UserPassword == userVM.Password).FirstOrDefault();
          ...
...........
        return View(userVM);
    }

@model UsersViewModel    
    <div >
        <h2>Login</h2>
        <form id="AccountLogin" method="post" asp-controller="Accounts" asp-action="Login">
            <div asp-validation-summary="All" >
                <div >
                    <input id="uName" asp-for="@Model.UserName" type="text" name="UserName" required="">
                    <label>Username</label>
                    <span asp-validation-for="UserName" ></span>
                </div>
                <div >
                    <input id="pWord" asp-for="@Model.Password" type="password" name="Password" required="">
                    <label>Password</label>
                    <span asp-validation-for="UserPassword" ></span>
                </div>
                <a asp-controller="Accounts" asp-action="Login">            
                    <span></span>
                    <span></span>
                    <span></span>
                    <span></span>
                    <input type="submit" value="Login"  
                        style="opacity: 1; border:0; font-size:16px; text-transform:uppercase; letter-spacing: 2px"/>
                </a>
            </div>
        </form>
    </div>

If you change it to , you can get the data you want.

  • Related