示例#1
0
        static void Main(string[] args)
        {
            var password     = "******";
            var securityData = SecurityData.CrearNuevo(password);


            var plainText = "Testing this thing out. Will it work? Will it not? More on this on the next episode. Stay tuned!";

            var iv = new byte[16];

            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(iv);
            }


            var newPassword = "******";

            var encryptedData = securityData.Encrypt(Encoding.UTF8.GetBytes(plainText), password, iv);

            Console.WriteLine("encryptedData: " + Convert.ToBase64String(encryptedData));
            securityData.PasswordChange(password, newPassword);

            var decryptedData = securityData.Decrypt(encryptedData, newPassword, iv);


            Console.WriteLine("decryptedData = " + Encoding.UTF8.GetString(decryptedData));
        }
        public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(StatusCode(400));
            }
            var user = await GetCurrentUserAsync();

            if (user != null)
            {
                var result = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);

                if (result.Succeeded)
                {
                    _logger.LogInformation(3, "User changed their password successfully.");

                    if (user.DatosSeguridad == null)
                    {
                        //El usuario no posería correctamente configurados los datos de seguridad para la clave privada. Creamos uno nuevo.
                        var securityData = SecurityData.CrearNuevo(model.NewPassword);
                        user.DatosSeguridad = securityData.SecurityString;
                    }
                    else
                    {
                        var securityData = new SecurityData(user.DatosSeguridad);
                        securityData.PasswordChange(model.OldPassword, model.NewPassword);
                        user.DatosSeguridad = securityData.SecurityString;
                    }

                    await _userManager.UpdateAsync(user);

                    return(Ok(new { success = true, message = ManageMessageId.ChangePasswordSuccess }));
                }
                return(Ok(new Response(false, result.Errors.Select(e => e.Description).Aggregate((current, next) => current + " " + next))));
            }
            return(Ok(new Response(false, ManageMessageId.Error.ToString())));
        }