After following a tutorial, I successfully created my first register login system in dot net and angular. The issue I encountered is that the author used static data in the tutorial's code example. However, I want to implement my own database data. As of now, I am uncertain about how to construct an if statement for accessing my database.
//Login
[HttpPost("login/")]
public IActionResult Login([FromBody]Benutzer user)
{
if(user == null)
{
return BadRequest();
}
if(user.benutzername == "123123" && user.passwort == "123")
{
var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("superSecretKey@345"));
var signingCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256);
var tokenOptions = new JwtSecurityToken
(
issuer: "http://localhost:5000",
audience: "http://localhost:5000",
claims: new List<Claim>(),
expires: DateTime.Now.AddMinutes(5),
signingCredentials: signingCredentials
);
var tokenString = new JwtSecurityTokenHandler().WriteToken(tokenOptions);
return Ok(new { Token = tokenString });
}
return Unauthorized();
}
I plan to start by searching for the user using their username:
var currentUserName = _context.Benutzer.FindAsync(user.benutzername);
However, I'm stuck on how to retrieve the specific password from the database to compare it.