'How can I authenticate user and how can I get first name and second name of user according to the login person?

Below is my login function, I want to change the following:

  1. differentiate between admin login and user login to show different page layouts
  2. get the user's first name and second name according to the login user

please how can I do that any suggestion or examples?

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(Customers customer)
    {
        if (ModelState.IsValid)
        {
            var user = await _context.Customers.SingleOrDefaultAsync(m => m.Email == customer.Email && m.Password == customer.Password);
            if (user != null)

            {
                

                return RedirectToAction("Account", "Account");
            }
            else 
            {
                ViewBag.error = "Login Failed";
                return RedirectToAction("Login");
            }
        }
        else
        {
            
            return RedirectToAction("Index", "Home");
        }
    }


Solution 1:[1]

  • You need to maintain Role column values in login table in DB.
  • Add values to the Role column Ex : Admin Role - 1 , User - 0
  • Change the login procedure to return the Role,FirstName,LastName instead of count
Select Role,Password,FirstName,LastName from Users where Username=@Username and Password=@Password
  • After authenticating the user(valid authentication, if password matches), redirect to the View based on the role.
if (Validate_User(Customers customer))
{
    if (customer.Role == "1")
    {
        return View("AdminView");
    }
    else
    {
        return View("UserView");
    }
    ViewBag.FirstName=customer.FirstName;
    ViewBag.LastName=customer.LastName;
}
else
{
    //handle failed login    
     ViewBag.error = "Login Failed";
     return RedirectToAction("Login");
}

Please refer Role Based Access

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HarshithaVeeramalla-MT