public IActionResult Update(ParkingLotViewModel model) { if (ModelState.IsValid) { ResponseDetails response = _apiHelper.SendApiRequest(model, "parkinglot/update", HttpMethod.Post); if (response.Success) { return(RedirectToAction("Details", new { id = _dataProtector.Protect(model.Id) })); } else { ErrorViewModel errorModel = new ErrorViewModel { Message = response.Data.ToString() }; return(View("Error", errorModel)); } } else { ModelState.AddModelError("Error", "Validation Error"); return(View(model)); } }
public IActionResult Create(CreateSlotModel model) { // TODO : learn how to validate model in pop up if (ModelState.IsValid) { SlotViewModel slot = new SlotViewModel() { IsBooked = false, ParkingLotId = model.ParkingLotId, SlotTypeId = model.SlotTypeId, HourlyRate = model.HourlyRate }; ResponseDetails response = _apiHelper.SendApiRequest(slot, "slot/add", HttpMethod.Post); if (response.Success) { return(RedirectToAction("Manage", new { Id = _dataProtector.Protect(model.ParkingLotId) })); } else { ErrorViewModel errorModel = new ErrorViewModel { Message = response.Data.ToString() }; return(View("Error", errorModel)); } } return(RedirectToAction("Manage", new { Id = _dataProtector.Protect(model.ParkingLotId) })); }
public IActionResult Confirm(ConfirmBookingModel model) { if (ModelState.IsValid) { string VehicalNumber = model.StateCode + "-" + model.DistrictCode + "-" + model.SeriesCode + " " + model.Number; model.StartDate = model.StartDate.AddHours(model.StartHour).AddMinutes(model.StartMinute); if ((DateTime.Now.Subtract(model.StartDate).TotalMinutes) > 0) { ModelState.AddModelError("", "Booking time must be after current moment."); return(View(model)); } DateTime EndDate = model.StartDate.AddHours(model.DurationHour).AddMinutes(model.DurationMinute); BookingViewModel bookingModel = new BookingViewModel() { CustomerId = model.UserId, SlotId = model.SlotId, StartDateTime = model.StartDate, EndDateTime = EndDate, VehicleNumber = VehicalNumber, IsActive = true, IsConfirmed = false, Amount = Convert.ToInt32((EndDate - model.StartDate).TotalHours * model.HourlyRate) }; ResponseDetails response = _apiHelper.SendApiRequest(bookingModel, "booking/add", HttpMethod.Post); if (response.Success) { bookingModel = JsonConvert.DeserializeObject <BookingViewModel> (response.Data.ToString()); _dataProtector.ProtectBookingRouteValues(bookingModel); return(RedirectToAction("Payment", new { id = _dataProtector.Protect(bookingModel.Id) })); } else { ErrorViewModel errorModel = new ErrorViewModel { Message = response.Data.ToString() }; return(View("Error", errorModel)); } } else { ModelState.AddModelError("", "Validation error"); return(View(model)); } }
private static void TestEncryption() { byte[] salt = new byte[] { 65, 61, 53, 222, 105, 5, 199, 241, 213, 56, 19, 120, 251, 37, 66, 185 }; byte[] data = new byte[255]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)i; } byte[] password = new byte[16]; for (int i = password.Length - 1; i >= 0; i--) { password[i] = (byte)i; } byte[] encrypted = CryptoUtility.AesEncryption(data, password, salt); byte[] decrypted = CryptoUtility.AesDecryption(encrypted, password, salt); if (!decrypted.SequenceEqual(data)) { throw new ApplicationException("AES encryption test fail"); } byte[] protectedData = DataProtector.Protect(salt); byte[] unprotectedData = DataProtector.Unprotect(protectedData); if (!unprotectedData.SequenceEqual(salt)) { throw new ApplicationException("Protected data API fail"); } }
// Wraps the common logic of working with a DataProtector instance. // 'protect' is TRUE if we're calling Protect, FALSE if we're calling Unprotect. private byte[] PerformOperation(byte[] data, bool protect) { // Since the DataProtector might depend on the impersonated context, we must // work with it only under app-level impersonation. The idea behind this is // that if the cryptographic routine is provided by an OS-level implementation // (like DPAPI), any keys will be locked to the account of the web application // itself. //SOURCE_CHANGED DataProtector dataProtector = null; try { dataProtector = _dataProtectorFactory.GetDataProtector(_purpose); return((protect) ? dataProtector.Protect(data) : dataProtector.Unprotect(data)); } finally { // These instances are transient IDisposable disposable = dataProtector as IDisposable; if (disposable != null) { disposable.Dispose(); } } }
public void CanProtectValue() { var protector = new DataProtector(); var protectedValue = protector.Protect("a value"); Assert.NotNull(protectedValue); Assert.AreNotEqual("a value", protectedValue); }
public void CanUnprotectValue() { var protector = new DataProtector(); var protectedValue = protector.Protect("a value"); var unprotectedValue = protector.Unprotect(protectedValue); Assert.NotNull(protectedValue); Assert.That(unprotectedValue, Is.EqualTo("a value")); }
static string ManuallyEncrypt(string stringtoEncrypt, IEnumerable <string> purposes = null) { var entropyCreator = new EntropyCreator(); var entropy = entropyCreator.CreateEntropy(purposes); var protector = new DataProtector(entropy); var userData = Encoding.UTF8.GetBytes(stringtoEncrypt); var cypher = protector.Protect(userData); return(Convert.ToBase64String(cypher)); }
public IActionResult Details(string id) { id = _dataProtector.UnprotectString(id); ErrorViewModel errorModel = new ErrorViewModel(); GetTransactionDetailsRequestBody requestBody = BuildGetTransactionDetailsModel(id); ResponseDetails response = _apiHelper.SendPaymentApiRequest(requestBody); if (response.Success) { GetTransactionDetailsResponseBody responseBody = JsonConvert.DeserializeObject <GetTransactionDetailsResponseBody> (response.Data.ToString()); if (responseBody.Messages.ResultCode.ToLower() == "ok") { TransactionDetailsModel model = new TransactionDetailsModel() { Transaction = responseBody.Transaction }; model.Transaction.EncryptedTransactionId = _dataProtector.Protect(model.Transaction.TransId); return(View(model)); } else { errorModel.Message = responseBody.Messages.Message.First().Text; } } else { errorModel.Message = response.Data.ToString(); } return(View("Error", errorModel)); }
public void AESEncryption() { byte[] salt = new byte[] { 65, 61, 53, 222, 105, 5, 199, 241, 213, 56, 19, 120, 251, 37, 66, 185 }; byte[] data = new byte[255]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)i; } byte[] password = new byte[16]; for (int i = password.Length - 1; i >= 0; i--) { password[i] = (byte)i; } byte[] encrypted = CryptoUtility.AesEncryption(data, password, salt); byte[] decrypted = CryptoUtility.AesDecryption(encrypted, password, salt); Assert.IsTrue(decrypted.SequenceEqual(data)); byte[] protectedData = DataProtector.Protect(salt); byte[] unprotectedData = DataProtector.Unprotect(protectedData); Assert.IsTrue(unprotectedData.SequenceEqual(salt)); }
private byte[] PerformOperation(byte[] data, bool protect) { byte[] result; //using (new ApplicationImpersonationContext()) { DataProtector dataProtector = null; try { dataProtector = this._dataProtectorFactory.GetDataProtector(this._purpose); result = (protect ? dataProtector.Protect(data) : dataProtector.Unprotect(data)); } finally { IDisposable disposable = dataProtector as IDisposable; if (disposable != null) { disposable.Dispose(); } } } return(result); }
private string ProtectString(string str) { return(Convert.ToBase64String(DataProtector.Protect(Encoding.UTF8.GetBytes(str)))); }
public void ProtectNullValueReturnsNull() { var protector = new DataProtector(); Assert.IsNull(protector.Protect(null)); }