// Post api/userinfo public IHttpActionResult Post(AuthorizationViewModel obj) { var encryptedGuid = Encryptor.EncryptAuthorization(obj).GuId; var auth = AuthRepo.Get(t => t.GuId == encryptedGuid).FirstOrDefault(); if (auth == null) { _logger.Debug($"{GetType().Name}, Post(), Error: Invalid authorization, guid: {encryptedGuid} "); return(new CustomErrorActionResult(Request, "Invalid authorization", ErrorCodes.InvalidAuthorization, HttpStatusCode.Unauthorized)); } var profile = AutoMapper.Mapper.Map <ProfileViewModel>(auth.Profile); profile = Encryptor.DecryptProfile(profile); var currentTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; profile.Employments = profile.Employments.AsQueryable().Where(x => x.StartDateTimestamp < currentTimestamp && (x.EndDateTimestamp > currentTimestamp || x.EndDateTimestamp == 0)).ToList(); var authModel = new AuthorizationViewModel { GuId = auth.GuId }; profile.Authorization = Encryptor.DecryptAuthorization(authModel); var currentYear = DateTime.Now.Year; var ui = new UserInfoViewModel { profile = profile, rates = AutoMapper.Mapper.Map <List <RateViewModel> >(RateRepo.Get().Where(x => x.Year == currentYear.ToString() && x.isActive).ToList()) }; try { Auditlog(auth.UserName, System.Reflection.MethodBase.GetCurrentMethod().Name, obj); } catch (Exception e) { _logger.Error($"{GetType().Name}, Post(), Auditlogging failed", e); return(InternalServerError()); } return(Ok(ui)); }
// POST api/auth public IHttpActionResult Post(AuthRequestViewModel obj) { try { var users = AuthRepo.Get(); UserAuth user = null; foreach (var u in users) { var decryptedUserName = Encryptor.DecryptUserName(u.UserName); if (decryptedUserName.Equals(obj.UserName, StringComparison.CurrentCultureIgnoreCase)) { user = u; } } var auth = Encryptor.EncryptAuthRequest(obj); if (user == null || user.Password != GetHash(user.Salt, obj.Password) || user.Profile.IsActive == false) { _logger.Debug($"{GetType().Name}, Post(), Username or password is incorrect for user: "******"Username or password is incorrect", ErrorCodes.IncorrectUserNameOrPassword, HttpStatusCode.Unauthorized)); } var profile = AutoMapper.Mapper.Map <ProfileViewModel>(user.Profile); profile = Encryptor.DecryptProfile(profile); var currentTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; profile.Employments = profile.Employments.AsQueryable().Where(x => x.StartDateTimestamp < currentTimestamp && (x.EndDateTimestamp > currentTimestamp || x.EndDateTimestamp == 0)).ToList(); var authModel = new AuthorizationViewModel { GuId = user.GuId }; profile.Authorization = Encryptor.DecryptAuthorization(authModel); var currentYear = DateTime.Now.Year; var ui = new UserInfoViewModel { profile = profile, rates = AutoMapper.Mapper.Map <List <RateViewModel> >(RateRepo.Get().Where(x => x.Year == currentYear.ToString() && x.isActive).ToList()) }; //Auditlogging try { Auditlog(auth.UserName, System.Reflection.MethodBase.GetCurrentMethod().Name, "username/password"); } catch (Exception e) { _logger.Error($"{GetType().Name}, Post(), Auditlogging failed", e); return(InternalServerError()); // Method not allowed to continue if auditlogging fails. } return(Ok(ui)); } catch (Exception e) { _logger.Error($"{GetType().Name}, Post(), Post method failed", e); throw; } }