Jwt Api controller:
=====================
[HttpPost("gettoken")] public IActionResult Login([FromBody]User user) { IActionResult response = Unauthorized(); var users = AuthenticateUser(user); if (user != null) { var tokenString = GenerateJSONWebToken(user); response = Ok(new { token = tokenString }); }
return response; }
private UserView AuthenticateUser(User login) { var login = (from u in _context.Users select new { Username = u.Username, Password = u.Password });
UserView userview=new UserView(login); return userview; }
private string GenerateToken(User model, int expireMinutes = 20) { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_JWTSettings.Secret); var now = DateTime.UtcNow; var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, Convert.ToString(model.Username)), // new Claim(ClaimTypes. Convert.ToString(model.token)) }),
Expires = now.AddMinutes(Convert.ToInt32(expireMinutes)),
SigningCredentials = new SigningCredentials( new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) };
var stoken = tokenHandler.CreateToken(tokenDescriptor); var token = tokenHandler.WriteToken(stoken);
return token; }
===>DB table: User
Hi Team, Above is my JWT authentication code to authorise a web api. The query what I have written in the AuthenticateUser is not working properly. If I tried to add a Username and password and hit the request in postman it say "NOT FOUND".
Can anyone help me where I am going wrong?
Thanks