Example #1
0
        public static void UpdateWebConfig(bool isProtectData, bool isMySql, string connectionString)
        {
            var configFilePath = PathUtils.MapPath("~/web.config");

            var doc = new XmlDocument();
            doc.Load(configFilePath);
            var dirty = false;
            var appSettings = doc.SelectSingleNode("configuration/appSettings");
            if (appSettings != null)
            {
                foreach (XmlNode setting in appSettings)
                {
                    if (setting.Name == "add")
                    {
                        var attrKey = setting.Attributes?["key"];
                        if (attrKey != null)
                        {
                            if (StringUtils.EqualsIgnoreCase(attrKey.Value, NameIsProtectData))
                            {
                                var attrValue = setting.Attributes["value"];
                                if (attrValue != null)
                                {
                                    attrValue.Value = isProtectData.ToString();
                                    dirty = true;
                                }
                            }
                            else if (StringUtils.EqualsIgnoreCase(attrKey.Value, NameDatabaseType))
                            {
                                var attrValue = setting.Attributes["value"];
                                if (attrValue != null)
                                {
                                    attrValue.Value = isMySql ? "MySql" : "SqlServer";
                                    if (isProtectData)
                                    {
                                        attrValue.Value = TranslateUtils.EncryptStringBySecretKey(attrValue.Value);
                                    }
                                    dirty = true;
                                }
                            }
                            else if (StringUtils.EqualsIgnoreCase(attrKey.Value, NameConnectionString))
                            {
                                var attrValue = setting.Attributes["value"];
                                if (attrValue != null)
                                {
                                    attrValue.Value = connectionString;
                                    if (isProtectData)
                                    {
                                        attrValue.Value = TranslateUtils.EncryptStringBySecretKey(attrValue.Value);
                                    }
                                    dirty = true;
                                }
                            }
                        }
                    }
                }
            }

            if (dirty)
            {
                var writer = new XmlTextWriter(configFilePath, System.Text.Encoding.UTF8)
                {
                    Formatting = Formatting.Indented
                };
                doc.Save(writer);
                writer.Flush();
                writer.Close();
            }

            IsMySql = isMySql;
            ConnectionString = connectionString;
            _helper = null;
        }
Example #2
0
        static WebConfigUtils()
        {
            var physicalApplicationPath = string.Empty;
            var applicationPath = string.Empty;
            if (HttpContext.Current != null)
            {
                physicalApplicationPath = HttpContext.Current.Request.PhysicalApplicationPath;
                applicationPath = HttpContext.Current.Request.ApplicationPath;
            }
            else
            {
                physicalApplicationPath = Environment.CurrentDirectory;
            }

            if (string.IsNullOrEmpty(applicationPath))
            {
                applicationPath = "/";
            }

            PhysicalApplicationPath = physicalApplicationPath;
            ApplicationPath = applicationPath;

            var isProtectData = false;
            var databaseType = string.Empty;
            var connectionString = string.Empty;
            try
            {
                var doc = new XmlDocument();

                var configFile = PathUtils.Combine(PhysicalApplicationPath, "web.config");

                doc.Load(configFile);

                var appSettings = doc.SelectSingleNode("configuration/appSettings");
                if (appSettings != null)
                {
                    foreach (XmlNode setting in appSettings)
                    {
                        if (setting.Name == "add")
                        {
                            var attrKey = setting.Attributes?["key"];
                            if (attrKey != null)
                            {
                                if (StringUtils.EqualsIgnoreCase(attrKey.Value, NameIsProtectData))
                                {
                                    var attrValue = setting.Attributes["value"];
                                    if (attrValue != null)
                                    {
                                        isProtectData = TranslateUtils.ToBool(attrValue.Value);
                                    }
                                }
                                else if (StringUtils.EqualsIgnoreCase(attrKey.Value, NameDatabaseType))
                                {
                                    var attrValue = setting.Attributes["value"];
                                    if (attrValue != null)
                                    {
                                        databaseType = attrValue.Value;
                                    }
                                }
                                else if (StringUtils.EqualsIgnoreCase(attrKey.Value, NameConnectionString))
                                {
                                    var attrValue = setting.Attributes["value"];
                                    if (attrValue != null)
                                    {
                                        connectionString = attrValue.Value;
                                    }
                                }
                            }
                        }
                    }

                    if (isProtectData)
                    {
                        databaseType = TranslateUtils.DecryptStringBySecretKey(databaseType);
                        connectionString = TranslateUtils.DecryptStringBySecretKey(connectionString);
                    }
                }
            }
            catch
            {
                // ignored
            }

            IsMySql = StringUtils.EqualsIgnoreCase(databaseType, "MySql");
            ConnectionString = connectionString;
        }
Example #3
0
 public static string GetTrueOrFalseImageHtml(string isDefaultStr)
 {
     return(GetTrueOrFalseImageHtml(TranslateUtils.ToBool(isDefaultStr)));
 }