Exemplo n.º 1
0
        private string CallEncryptOrDecrypt(bool doEncrypt, string xmlString, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
        {
            string str = null;
            WindowsImpersonationContext context    = null;
            string           assemblyQualifiedName = protectionProvider.GetType().AssemblyQualifiedName;
            ProviderSettings settings = protectedConfigSection.Providers[protectionProvider.Name];

            if (settings == null)
            {
                throw System.Web.Util.ExceptionUtil.ParameterInvalid("protectionProvider");
            }
            NameValueCollection parameters = settings.Parameters;

            if (parameters == null)
            {
                parameters = new NameValueCollection();
            }
            string[] allKeys         = parameters.AllKeys;
            string[] parameterValues = new string[allKeys.Length];
            for (int i = 0; i < allKeys.Length; i++)
            {
                parameterValues[i] = parameters[allKeys[i]];
            }
            if (this._Identity != null)
            {
                context = this._Identity.Impersonate();
            }
            try
            {
                try
                {
                    IRemoteWebConfigurationHostServer o = CreateRemoteObject(this._Server, this._Username, this._Domain, this._Password);
                    try
                    {
                        str = o.DoEncryptOrDecrypt(doEncrypt, xmlString, protectionProvider.Name, assemblyQualifiedName, allKeys, parameterValues);
                    }
                    finally
                    {
                        while (Marshal.ReleaseComObject(o) > 0)
                        {
                        }
                    }
                    return(str);
                }
                finally
                {
                    if (context != null)
                    {
                        context.Undo();
                    }
                }
            }
            catch
            {
            }
            return(str);
        }
        private string CallEncryptOrDecrypt(bool doEncrypt, string xmlString, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
        {
#if !FEATURE_PAL // FEATURE_PAL has no COM objects => no encryption
            // ROTORTODO: COM Objects are not implemented.
            // CORIOLISTODO: COM Objects are not implemented.
            ProviderSettings    ps;
            NameValueCollection nvc;
            string  []          paramKeys;
            string  []          paramValues;
            string returnString = null;
            string typeName;
            WindowsImpersonationContext wiContext = null;

            ////////////////////////////////////////////////////////////
            // Step 1: Create list of parameters for the protection provider
            typeName = protectionProvider.GetType().AssemblyQualifiedName;
            ps       = protectedConfigSection.Providers[protectionProvider.Name];
            if (ps == null)
            {
                throw ExceptionUtil.ParameterInvalid("protectionProvider");
            }

            nvc = ps.Parameters;
            if (nvc == null)
            {
                nvc = new NameValueCollection();
            }

            paramKeys   = nvc.AllKeys;
            paramValues = new string[paramKeys.Length];
            for (int iter = 0; iter < paramKeys.Length; iter++)
            {
                paramValues[iter] = nvc[paramKeys[iter]];
            }

            ////////////////////////////////////////////////////////////
            // Step 2: Set the impersonation if required
            if (_Identity != null)
            {
                wiContext = _Identity.Impersonate();
            }

            try {
                try {
                    //////////////////////////////////////////////////////////////////
                    // Step 3: Get the type and create the object on the remote server
                    IRemoteWebConfigurationHostServer remoteSrv = CreateRemoteObject(_Server, _Username, _Domain, _Password);
                    try {
                        //////////////////////////////////////////////////////////////////
                        // Step 4: Call the API
                        returnString = remoteSrv.DoEncryptOrDecrypt(doEncrypt, xmlString, protectionProvider.Name, typeName, paramKeys, paramValues);
                    } finally {
                        while (Marshal.ReleaseComObject(remoteSrv) > 0)
                        {
                        }                                                   // release the COM object
                    }
                } finally {
                    if (wiContext != null)
                    {
                        wiContext.Undo(); // revert impersonation
                    }
                }
            }
            catch {
            }

            return(returnString);
#else       // !FEATURE_PAL
            throw new NotImplementedException("ROTORTODO");
#endif      // !FEATURE_PAL
        }