public async Task <Result> LoginAsync(ModelLoginCredentials credential) { Result result = new Result(); using (IDbConnection conn = Connection) { try { string sQuery = @"SELECT * FROM UserCredentials WHERE Email = @Email"; conn.Open(); var match = await conn.QueryAsync <ModelUserCredentials>(sQuery, new { Email = credential.Email }); var currentuser = match.FirstOrDefault(); // user does not exit if (currentuser == null) { result.StatusCode = ResultCodes.AuthFail; result.Description = "User Name or Password does not match"; return(result); } // checking account status var checkresult = checkAccountStatus(currentuser); if (checkresult.StatusCode != ResultCodes.Success) { return(checkresult); } var validate = Hash.Validate(credential.Password, currentuser.Salt, currentuser.Password); //checking username pwd if (validate) { // authentication successful so generate jwt token var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(JWTSecret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, currentuser.UserID.ToString()), new Claim("Email", currentuser.Email), new Claim("UserType", currentuser.UserType.ToString()) }), Audience = ValidAudience, Issuer = ValidIssuer, Expires = DateTime.UtcNow.AddMinutes(TokenLifeMinutes), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; // change login status setLoginStatus(currentuser.UserID, 0); var token = tokenHandler.CreateToken(tokenDescriptor); result.Data = tokenHandler.WriteToken(token); return(result); } else { setLoginStatus(currentuser.UserID, currentuser.LoginAttempt + 1); result.StatusCode = ResultCodes.AuthFail; result.Description = "User Name or Password does not match"; return(result); } } catch (Exception ex) { result.StatusCode = ResultCodes.Error; result.Description = ex.Message; return(result); } } }
public async Task <ActionResult <Result> > Login(ModelLoginCredentials cred) { Console.WriteLine("Login"); return(await _credRepo.LoginAsync(cred)); }