Example #1
0
        public ActionResult Login([FromBody] LoginInputModel inputModel)
        {
            try
            {
                User foundUser = UserService.Login(inputModel);

                if (foundUser == null)
                {
                    return(Unauthorized());
                }

                var tokenString = AuthenticationHelper.GenerateJWTToken(foundUser);

                return(Ok(new
                {
                    ID = foundUser.ID,
                    Username = foundUser.Username,
                    Role = foundUser.UserRole,
                    Token = tokenString
                }));
            }
            catch (UnauthorizedAccessException ex)
            {
                return(Unauthorized(ex.Message));
            }
        }
Example #2
0
 public string GenerateJWTToken(User foundUser)
 {
     if (foundUser == null)
     {
         throw new ArgumentException("User can't be null");
     }
     if (foundUser.Password == null || foundUser.Salt == null)
     {
         throw new ArgumentException("User is missing a password or salt");
     }
     return(AuthenticationHelper.GenerateJWTToken(foundUser));
 }