public async Task <ServiceResponse <UserDto> > Login(LoginDto login) { ServiceResponse <UserDto> serviceResponse = new ServiceResponse <UserDto>(); PasswordHashing ph = new PasswordHashing(); User entity = new User(); try { entity = _context.Users.First(u => u.Username == login.Username); if (ph.IsValid(login.Password, entity.Salt, entity.Password)) { serviceResponse.Data = _mapper.Map <UserDto>(entity); } else { throw new Exception(""); } if (entity.EnforcePasswordChange == true) { throw new Exception("Password should be changed!"); } } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message = "Wrong username or password.\n"; serviceResponse.Message += (ex.InnerException != null) ? ex.InnerException.Message : ex.Message; } return(serviceResponse); }
public async Task <ServiceResponse <UserDto> > PasswordChange(PasswordChangeDto pwdChange) { ServiceResponse <UserDto> serviceResponse = new ServiceResponse <UserDto>(); PasswordHashing ph = new PasswordHashing(); User entity = new User(); try { entity = _context.Users.First(u => u.Username == pwdChange.Username); if (ph.IsValid(pwdChange.CurrentPassword, entity.Salt, entity.Password)) { entity.Salt = Encoding.Unicode.GetString(ph.GetSalt()); entity.Password = Encoding.Unicode.GetString(ph.GetKey(pwdChange.NewPassword, Encoding.Unicode.GetBytes(entity.Salt))); entity.LastPasswordChange = DateTime.Now; entity.EnforcePasswordChange = false; _context.SaveChanges(); serviceResponse.Data = _mapper.Map <UserDto>(entity); } else { throw new Exception("Wrong current password!"); } } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message; } return(serviceResponse); }
public async Task <ServiceResponse <GetUserDto> > Login(LoginDto login) { ServiceResponse <GetUserDto> serviceResponse = new ServiceResponse <GetUserDto>(); PasswordHashing ph = new PasswordHashing(); User entity = new User(); try { entity = _context.Users.First(u => u.Username == login.Username); if (ph.IsValid(login.Hash, entity.Salt, entity.Hash)) { serviceResponse.Data = _mapper.Map <GetUserDto>(_context.Users.Include(u => u.UserBooks).FirstOrDefault(u => u.Id == entity.Id)); } else { throw new Exception(""); } } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message += (ex.InnerException != null) ? ex.InnerException.Message : ex.Message; if (serviceResponse.Message == "") { serviceResponse.Message = "Niepoprawny login lub hasło."; } } return(serviceResponse); }