public HttpResponseMessage ResetPass(ResetPassRequest request) { try { var decrypted = EncryptorText.DataDecrypt(request.Token.Replace("!!", "/").Replace("$", "+")); if (string.IsNullOrEmpty(request.Token) || string.IsNullOrEmpty(decrypted)) { return(CreateErrorResponse(HttpStatusCode.BadRequest, "Token de recuperación no encontrado.")); } string id = decrypted.Split('@').First(); var user = _userService.FindBy(x => x.Uuid == id).First(); if (user == null || DateUtil.GetDateTimeNow() > user.ExpiraToken) { return(CreateErrorResponse(HttpStatusCode.BadRequest, "El token ha expirado.")); } if (user.Role.Code != Constants.ROLE_DEFAULT_API) { return(CreateErrorResponse(HttpStatusCode.BadRequest, "El usuario no cuenta con acceso.")); } user.Password = request.Password; _userService.Update(user); return(CreateResponse("OK", "Datos actualizados correctamente")); } catch (Exception e) { return(CreateErrorResponse(e)); } }
public async Task EncryptTest_EmtyText_Async() { // arange string actual; string key = "скорпион"; // act EncryptorText encryptorText = new EncryptorText(); actual = await encryptorText.Encrypt(string.Empty, key); // assert Assert.AreEqual(string.Empty, actual); }
public async Task EncryptTest_KeyEmpty_Async() { // arange string actual; string key = ""; string dataDecrypted = await FileReader.getDataStringAsync(Path.GetFullPath(@"..\..\..\Resources\ANSI Length-1000000 decrypted.txt")); // act EncryptorText encryptorText = new EncryptorText(); actual = await encryptorText.Encrypt(dataDecrypted, key); // assert Assert.AreEqual(string.Empty, actual); }
public async Task DecryptTest_Key_bigger_Text_Async() { // arange string actual; string key = "йцукенгшщзхъфывапролджэячсмитьбю"; string dataDecrypted = "один два three четыре"; string dataEncrypted = "шъьш ипг three пюъркщ"; // act EncryptorText encryptorText = new EncryptorText(); actual = await encryptorText.Decrypt(dataEncrypted, key); // assert Assert.AreEqual(dataDecrypted, actual); }
public ActionResult ResetPassword(RecoverPasswordViewModel model) { if (!ModelState.IsValid) { return(Json(new { success = false, issue = model, errors = ModelState.Keys.Where(k => ModelState[k].Errors.Count > 0) .Select(k => new { propertyName = k, errorMessage = ModelState[k].Errors[0].ErrorMessage }) })); } try { var resultado = _userService.FindBy(e => e.Email == model.Email).First(); if (resultado != null) { ViewBag.mensajeError = string.Empty; resultado.ExpiraToken = System.DateTime.Now.AddDays(1); string token = (resultado.Uuid + "@" + DateTime.Now.AddDays(1).ToString()); token = EncryptorText.DataEncrypt(token).Replace("/", "!!").Replace("+", "$"); resultado.Token = token; Dictionary <string, string> customParams = new Dictionary <string, string>(); string urlAccion = (string)ConfigurationManager.AppSettings["_UrlServerAccess"]; string link = urlAccion + "Auth/AccedeToken?token=" + token; customParams.Add("param1", resultado.Email); customParams.Add("param2", link); NotificationUtil.SendNotification(resultado.Email, customParams, Constants.NOT_TEMPLATE_PASSWORDRECOVER); _userService.Update(resultado); AddViewMessage(TypeMessageView.SUCCESS, Messages.RequestSuccessful); return(View("Login")); } } catch (Exception ex) { //ErrorController.SaveLogError(this, listAction.Update, "RecuperarContrasena", ex); } ModelState.AddModelError("Email", "No se encontró ninguna cuenta con el correo proporcionado. Verifique su información."); return(Json(new { success = false, issue = model, errors = ModelState.Keys.Where(k => ModelState[k].Errors.Count > 0) .Select(k => new { propertyName = k, errorMessage = ModelState[k].Errors[0].ErrorMessage }) })); }
public async Task DecryptTest_1000000symbols_Async() { // arange string actual; string key = "скорпион"; string dataDecrypted = await FileReader.getDataStringAsync(Path.GetFullPath(@"..\..\..\Resources\ANSI Length-1000000 decrypted.txt")); string dataEncrypted = await FileReader.getDataStringAsync(Path.GetFullPath(@"..\..\..\Resources\ANSI Length-1000000 encrypted.txt")); // act EncryptorText encryptorText = new EncryptorText(); actual = await encryptorText.Decrypt(dataEncrypted, key); // assert Assert.AreEqual(dataDecrypted, actual); }
public ActionResult AccedeToken(string token) { try { if (string.IsNullOrEmpty(token)) { return(RedirectToAction("Login")); } var desencriptaToken = EncryptorText.DataDecrypt(token.Replace("!!", "/").Replace("$", "+")); if (string.IsNullOrEmpty(desencriptaToken)) { return(RedirectToAction("Login")); } var elements = desencriptaToken.Split('@'); string id = elements.First().ToString(); var resultado = _userService.FindBy(e => e.Uuid == id).First(); int[] valores = new int[100]; for (int a = 0; a < 100; a++) { valores[a] = a++; } if (resultado != null && DateTime.Now <= resultado.ExpiraToken) { ResetPassword model = new ResetPassword(); model.Uuid = resultado.Uuid.ToString(); return(View("ResetPassword", model)); } } catch (Exception ex) { AddViewMessage(TypeMessageView.WARNING, Messages.TokenExpired); return(View("Login")); //ErrorController.SaveLogError(this, listAction.Update, "AccedeToken", ex); } AddViewMessage(TypeMessageView.INFO, Messages.TokenError); return(View("Login")); }
public HttpResponseMessage Recover([FromUri(Name = "email")] string email) { try { var user = _userService.FindBy(e => e.Email == email).FirstOrDefault(); if (user == null) { return(CreateErrorResponse(HttpStatusCode.BadRequest, "El correo electrónico solicitado no se encuentra registrado.")); } if (user.Role.Code != Constants.ROLE_DEFAULT_API) { return(CreateErrorResponse(HttpStatusCode.BadRequest, "El usuario no cuenta con acceso al API")); } string token = (user.Uuid + "@" + DateTime.Now.AddDays(1).ToString()); token = EncryptorText.DataEncrypt(token).Replace("/", "!!").Replace("+", "$"); List <string> Email = new List <string>(); Email.Add(user.Email); Dictionary <string, string> customParams = new Dictionary <string, string>(); string urlAccion = ConfigurationManager.AppSettings["_UrlServerAccess"].ToString(); string link = urlAccion + "Auth/AccedeToken?token=" + token; customParams.Add("param1", user.Email); customParams.Add("param2", link); string template = "aa61890e-5e39-43c4-92ff-fae95e03a711"; NotificationUtil.SendNotification(Email, customParams, template); user.ExpiraToken = DateUtil.GetDateTimeNow().AddDays(1); user.Token = token; _userService.Update(user); return(CreateResponse("OK", "Datos devueltos correctamente")); } catch (Exception e) { return(CreateErrorResponse(e)); } }