public IActionResult TokenGeneration(string name, string password) { _log4net.Info("Trying to Login"); User user = new User { Name = name, Password = password }; //check if user exist using the method Authentication() in JwtAuthenticationRepository bool isUserExist = authenticationRepository.Authentication(user.Name, user.Password); if (!isUserExist) { _log4net.Warn("Unauthorised Access !!! Check user credentials"); return(new UnauthorizedResult()); } var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwtoken:SecretKey"])); var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); var claims = new List <Claim> { new Claim("UserId", user.UserId.ToString()) }; var token = new JwtSecurityToken( issuer: configuration["Jwtoken:Issuer"], audience: configuration["Jwtoken:Audience"], claims: claims, expires: DateTime.Now.AddSeconds(30), signingCredentials: credentials); return(new OkObjectResult(new JwtSecurityTokenHandler().WriteToken(token))); }
//Check if user exist public bool Authentication(string name, string password) { return(_authenticationRepository.Authentication(name, password)); }