I'm writing a test web app in ASP.net core MVC and entity framework, I've built a login view and want to authenticate a user, with username and password against my users database table. This is for an in house app with max 10 users. I'm not using the Microsoft identity or user registrations. What would be the best way of approaching this basic user authentication?
Many thanks.
I'm getting an error with my UserService as it doesn't exist in the current context, and I'm getting an error with my ClaimTypes as it cannot convert from string to System.IO.BinaryReader
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> LoginUser(string email)
{
var user = await UserService.GetUserByEmail(email);
if (user == null)
{
TempData["Login failed"] = $"Login for {email} failed.";
return this.Redirect("/Login");
}
var claims = new List<Claim>
{
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Name, user.Name)
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(ClaimsIdentity));
return Redirect("/");
}
}