When I try to create token and expiration SecurityTokenDescriptor for a syntax it doesn't generate an error but when I call this function on postman it gives that error "System.NullReferenceException: Object reference not set to an instance of an object." My code below here.
[HttpPost]
[Route("Login")]
public async Task<ActionResult<KullaniciModel>> Authenticate(KullaniciLoginDTO kullaniciLoginDTO)
{
KullaniciModel kullaniciModel = new KullaniciModel();
var user = await _context.KullaniciModel.SingleOrDefaultAsync(x=> x.kullaniciEPosta == kullaniciLoginDTO.kullaniciEPosta);
if(HashingHelper.VerifyPassword(kullaniciLoginDTO.sifre, user.passwordhash, user.passwordsalt )) // sifre means password
{
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim("kullaniciId", user.kullaniciId.ToString()), //kullaniciId means userID
new Claim("kullaniciRol", user.kullaniciRol.ToString()),
}),
Expires = DateTime.UtcNow.AddMinutes(75),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_appSettings.JWT_Secret)), SecurityAlgorithms.HmacSha256Signature)
};
var tokenHandler = new JwtSecurityTokenHandler();
var securityToken = tokenHandler.CreateToken(tokenDescriptor);
var token = tokenHandler.WriteToken(securityToken);
return Ok(new { token });
}
else
{
return BadRequest(new { message = "Kullanıcı Girişi Yapılamadı" });
}
}
At this part "var tokenDescriptor = new SecurityTokenDescriptor" generates an error that is shown above.
What was the problem I couldn't find any solution to that