Ejemplo n.º 1
0
        private Func <Purpose, DataProtector> GetDataProtectorFactory()
        {
            string applicationName       = _machineKeySection.ApplicationName;
            string dataProtectorTypeName = _machineKeySection.DataProtectorType;

            Func <Purpose, DataProtector> factory = purpose => {
                // Since the custom implementation might depend on the impersonated
                // identity, we must instantiate it under app-level impersonation.
                using (new ApplicationImpersonationContext()) {
                    return(DataProtector.Create(dataProtectorTypeName, applicationName, purpose.PrimaryPurpose, purpose.SpecificPurposes));
                }
            };

            // Invoke the factory once to make sure there aren't any configuration errors.
            Exception factoryCreationException = null;

            try {
                DataProtector dataProtector = factory(_creationTestingPurpose);
                if (dataProtector != null)
                {
                    IDisposable disposable = dataProtector as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                    return(factory); // we know at this point the factory is good
                }
            }
            catch (Exception ex) {
                factoryCreationException = ex;
            }

            // If we reached this point, there was a failure:
            // the factory returned null, threw, or did something else unexpected.
            throw ConfigUtil.MakeConfigurationErrorsException(
                      message: SR.GetString(SR.MachineKeyDataProtectorFactory_FactoryCreationFailed),
                      innerException: factoryCreationException, // can be null
                      configProperty: _machineKeySection.ElementInformation.Properties["dataProtectorType"]);
        }
Ejemplo n.º 2
0
        /// <summary> Executes the command. </summary>
        /// <returns> Return 0 is everything was right, an negative error code otherwise. </returns>
        protected override int ExecuteCommand()
        {
            if (Option.Equals("e", StringComparison.OrdinalIgnoreCase))
            {
                var protector = string.IsNullOrWhiteSpace(Entropy) ? DataProtector.Create() : DataProtector.Create(Entropy);
                try
                {
                    var encrypted = protector.Encrypt(Text);
                    Console.WriteLine("Encrypted: {0}", encrypted);
                    return(0);
                }
                catch (Exception ex)
                {
                    string message = ex.Message;
                    Console.WriteLine("An error was encountered: {0} {1}Details: {2}", message, Environment.NewLine, ex.StackTrace);
                    return(-1);
                }
            }

            if (Option.Equals("d", StringComparison.OrdinalIgnoreCase))
            {
                var protector = string.IsNullOrWhiteSpace(Entropy) ? DataProtector.Create() : DataProtector.Create(Entropy);
                try
                {
                    var decrypted = protector.Decrypt(Text);
                    Console.WriteLine("Decrypted: {0}", decrypted);
                    return(0);
                }
                catch (Exception ex)
                {
                    string message = ex.Message;
                    Console.WriteLine("An error was encountered: {0} {1}Details: {2}", message, Environment.NewLine, ex.StackTrace);
                    return(-1);
                }
            }

            Console.WriteError("Invalid option value. Supported options are e OR d");
            return(-1);
        }