示例#1
0
        /// <summary>
        /// Indicates whether the current request can be identified as an installation script
        /// </summary>
        /// <returns>True if the request is being made to the install directory, false otherwise.</returns>
        public static bool IsInstallRequest()
        {
            HttpRequest request = HttpContextHelper.SafeGetRequest();

            if (request != null)
            {
                // OBTAIN THE SCRIPT PATH FROM THE URL
                string absolutePath = request.Url.AbsolutePath.ToLowerInvariant();
                string relativePath;
                if (request.ApplicationPath.Length > 1)
                {
                    relativePath = absolutePath.Substring(request.ApplicationPath.Length);
                }
                else
                {
                    relativePath = absolutePath;
                }

                // SEE IF THIS IS AN INSTALL SCRIPT
                if (relativePath.StartsWith("/install/"))
                {
                    return(true);
                }
            }

            // AN INSTALL REQUEST WAS NOT DETECTED
            return(false);
        }
示例#2
0
        private void RemoveDataFromSession(string key)
        {
            HttpSessionState session = HttpContextHelper.SafeGetSession();

            if (session != null)
            {
                session.Remove(key);
            }
            else if (_InternalStorage.ContainsKey(key))
            {
                _InternalStorage.Remove(key);
            }
        }
示例#3
0
        private void SetDataInSession(string key, object value)
        {
            HttpSessionState session = HttpContextHelper.SafeGetSession();

            if (session != null)
            {
                session[key] = value;
            }
            else
            {
                _InternalStorage[key] = value;
            }
        }
示例#4
0
        private object GetDataFromSession(string key)
        {
            HttpSessionState session = HttpContextHelper.SafeGetSession();

            if (session != null)
            {
                return(session[key]);
            }
            else if (_InternalStorage.ContainsKey(key))
            {
                return(_InternalStorage[key]);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Update the database connection string
        /// </summary>
        /// <param name="connectionString">new connection string</param>
        /// <param name="encrypt"></param>
        /// <returns></returns>
        public static void UpdateConnectionString(string connectionString, bool encrypt)
        {
            HttpRequest request = HttpContextHelper.SafeGetRequest();

            if (request != null)
            {
                try
                {
                    System.Configuration.Configuration config  = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(request.ApplicationPath);
                    ConnectionStringsSection           section = config.GetSection("connectionStrings") as ConnectionStringsSection;
                    if (section.SectionInformation.IsProtected)
                    {
                        section.SectionInformation.UnprotectSection();
                    }
                    section.ConnectionStrings.Remove("LocalSqlServer");
                    section.ConnectionStrings["AbleCommerce"].ConnectionString = connectionString;
                    if (encrypt)
                    {
                        if (!section.SectionInformation.IsProtected)
                        {
                            section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                        }
                    }
                    else
                    {
                        if (section.SectionInformation.IsProtected)
                        {
                            section.SectionInformation.UnprotectSection();
                        }
                    }
                    config.Save();
                }
                catch
                {
                    //IF MS API FAILS, WRITE THE FILE CLEARTEXT
                    StringBuilder textBuilder = new StringBuilder();
                    textBuilder.AppendLine("<connectionStrings>");
                    textBuilder.AppendFormat("\t<add name=\"AbleCommerce\" connectionString=\"{0}\" providerName=\"System.Data.SqlClient\" />\r\n", connectionString);
                    textBuilder.AppendLine("</connectionStrings>");

                    string configPath = request.MapPath("~/App_Data/database.config");
                    File.WriteAllText(configPath, textBuilder.ToString());
                }
            }
        }
示例#6
0
        /// <summary>
        /// Performs an automated lookup at whatismyip.com to determine the external IP of the calling server
        /// </summary>
        /// <returns>A string with the external IP of the calling server</returns>
        public static string WhatIsMyIP()
        {
            // LOOK FOR CACHED IP INFORMATION
            Cache cache = HttpContextHelper.SafeGetCache();

            if (cache != null && cache["WhatIsMyIP"] != null)
            {
                return((string)cache["WhatIsMyIP"]);
            }

            // TRY TO GET IP FROM WEBSERVICE
            string response = string.Empty;

            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.whatismyip.com/automation/n09230945.asp");
                httpWebRequest.Method = "GET";
                using (StreamReader responseStream = new StreamReader(httpWebRequest.GetResponse().GetResponseStream(), System.Text.Encoding.UTF8))
                {
                    response = responseStream.ReadToEnd();
                    responseStream.Close();
                }
                if (!Regex.IsMatch(response, @"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"))
                {
                    response = string.Empty;
                }
            }
            catch { }

            // CACHE AND RETURN RESPONSE
            if (cache != null)
            {
                cache.Add("WhatIsMyIP", response, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0), CacheItemPriority.Normal, null);
            }
            return(response);
        }
        /// <summary>
        /// Decrypts the given encrypted text using the given key.
        /// </summary>
        /// <param name="cipherText">The encrypted text</param>
        /// <param name="keyBytes">The key to use</param>
        /// <returns>The decrypted text</returns>
        public static string DecryptAES(string cipherText, byte[] keyBytes)
        {
            //DO NOT DECRYPT EMPTY STRING
            if (string.IsNullOrEmpty(cipherText))
            {
                return(string.Empty);
            }

            //DO NOT DECRYPT IF THERE IS NO CRYPT KEY
            if ((keyBytes == null) || (keyBytes.Length == 0))
            {
                return(cipherText);
            }

            // STORAGE FOR DATA DECODED FROM BASE64
            byte[] ivPlusCipher;
            try
            {
                // DECODE THE BASE64 DATA
                ivPlusCipher = Convert.FromBase64String(cipherText);
            }
            catch (System.FormatException)
            {
                // DATA WAS NOT VALID BASE64, IT CANNOT BE DECRYPTED
                return(cipherText);
            }

            // THE DECRYPTED DATA MUST BE AT LEAST 17 BYTES (AND PROBABLY LONGER)
            if (ivPlusCipher.Length <= 16)
            {
                return(cipherText);
            }

            //CONVERT CIPHER TEXT TO BYTES
            try
            {
                //SPLIT THE IV (FIRST 128 BITS) AND CIPHER TEXT
                byte[] ivBytes         = new byte[16];
                byte[] cipherTextBytes = new byte[ivPlusCipher.Length - 16];
                System.Buffer.BlockCopy(ivPlusCipher, 0, ivBytes, 0, 16);
                System.Buffer.BlockCopy(ivPlusCipher, 16, cipherTextBytes, 0, cipherTextBytes.Length);

                //CONFIGURE AES
                RijndaelManaged symmetricKey = new RijndaelManaged();
                symmetricKey.Mode = CipherMode.CBC;
                symmetricKey.Key  = keyBytes;
                symmetricKey.IV   = ivBytes;

                //CREATE DECRYPTOR
                ICryptoTransform decryptor = symmetricKey.CreateDecryptor();

                //CREATE BUFFER TO HOLD DECRYPTED TEXT
                byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                int    decryptedByteCount;

                //CREATE MEMORY STREAM OF DECRYPTED DATA
                using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
                {
                    //CREATE THE CRYPTO STREAM
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        //DECRYPT THE CIPHER TEXT
                        decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);

                        //CLOSE CRYPTO STREAM
                        cryptoStream.Close();
                    }
                    //CLOSE MEMORY STREAM
                    memoryStream.Close();
                }

                //CONVERT DECRYPTED BYTES TO STRING
                string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);

                //RETURN DECRYPTED TEXT
                return(plainText);
            }
            catch (Exception ex)
            {
                //SOMETHING WENT WRONG, RETURN ORIGINAL VALUE
                string scriptName = HttpContextHelper.GetCurrentScriptName();
                Logger.Debug("Error decrypting value " + cipherText + " in script " + scriptName, ex);
                return(cipherText);
            }
        }