Exemple #1
0
        public UserAuthorizationModel Login(LoginModel loginModel)
        {
            if (string.IsNullOrEmpty(loginModel.Username))
            {
                throw new ValidationException(Messages.UsernameRequired);
            }

            if (string.IsNullOrEmpty(loginModel.Password))
            {
                throw new ValidationException(Messages.PasswordRequired);
            }

            var hashedPass = HashUtils.EncodeString(loginModel.Password);
            var user       = _userRepository.GetByCredentials(loginModel.Username, hashedPass);

            if (user == null)
            {
                throw new UnauthorizedException(Messages.InvalidLogin);
            }

            var authorization = new UserAuthorizationModel
            {
                Id       = user.Id,
                Username = user.Username,
                IsAdmin  = user.IsAdmin,
                Token    = SecurityUtils.GenerateToken()
            };

            AuthorizationCache.Instance.AddOrUpdateAuthorization(authorization);

            return(authorization);
        }