コード例 #1
0
        public override bool BindProperty(
            ControllerContext controllerContext,
            ModelBindingContext bindingContext,
            PropertyDescriptor propertyDescriptor)
        {
            var value =
                bindingContext.ValueProvider.GetValue($"{bindingContext.ModelName}.{propertyDescriptor.Name}") ??
                bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);

            var attemptedValue = value?.AttemptedValue;

            if (attemptedValue.IsNotNullOrEmpty())
            {
                try
                {
                    var decryptedData = EncryptionTools.Decrypt(
                        attemptedValue,
                        ConfigurationManager.AppSettings["EncryptKey"]);
                    var convertor = TypeDescriptor.GetConverter(propertyDescriptor.PropertyType);
                    propertyDescriptor.SetValue(bindingContext.Model, convertor.ConvertFrom(decryptedData));
                    return(true);
                }
                catch (Exception)
                {
                    bindingContext.ModelState.AddModelError(
                        propertyDescriptor.Name,
                        $"The field '{propertyDescriptor.Name}' has an invalid value in it!");
                    return(false);
                }
            }

            propertyDescriptor.SetValue(bindingContext.Model, bindingContext.ModelType.GetDefaultValue());
            return(true);
        }
コード例 #2
0
        // Get the contents of the ini file.
        public bool ReadAllEntries(ref ArrayList iniEntries, ref string errorMessage)
        {
            if (!File.Exists(iniPath))
            {
                errorMessage = "Ini file not found.";
                return(false);
            }

            if (useEncryption)
            {
                if (!ValidateEncryptedIniFile())
                {
                    errorMessage = "This file is not an encrypted ini file created by this class, or decryption failed / invalid key.";
                    return(false);
                }
            }

            try
            {
                // Read the ini file into an arraylist...
                StreamReader reader   = new StreamReader(iniPath);
                string       thisLine = "";
                String[]     parts;
                iniEntries.Clear();

                //Until eof
                while (!(reader.Peek() == -1))
                {
                    thisLine = reader.ReadLine();

                    if (useEncryption)
                    {
                        thisLine = EncryptionTools.Decrypt(thisLine, encKey, true);
                        parts    = Regex.Split(thisLine, keyValueSeparator);

                        if (!(parts[0].Equals("SYSTEM_ENCRYPTION_VALIDATOR") && parts[1].Equals("ENCRYPTION_OK")))
                        {
                            iniEntries.Add(thisLine);
                        }
                    }
                    else
                    {
                        iniEntries.Add(thisLine);
                    }
                }

                reader.Close();
            }
            catch (Exception)
            {
                // The file's empty.
            }

            return(true);
        }
コード例 #3
0
        public static Dictionary <string, string> GetTokenData(string token, string encryptKey)
        {
            token = Decode(token);
            var decryptedToken = EncryptionTools.Decrypt(token, encryptKey);

            return(decryptedToken.Split(ParameterDelimiter)
                   .Select(
                       item =>
            {
                var temp = item.Split(ValueDelimiter);
                return new KeyValuePair <string, string>(
                    temp[0],
                    temp.Length > 1 ? temp[1] : null);
            })
                   .ToDictionary(key => key.Key, val => val.Value));
        }
コード例 #4
0
        public bool ValidateEncryptedIniFile()
        {
            String thisLine = "";

            String[]     parts     = null;
            StreamReader reader    = null;
            bool         validated = false;

            try
            {
                // Read the ini file into an arraylist...
                reader = new StreamReader(iniPath);

                //Until eof

                while (reader.Peek() != -1)
                {
                    thisLine = reader.ReadLine();
                    thisLine = EncryptionTools.Decrypt(thisLine, encKey, true);

                    if (thisLine.Contains(keyValueSeparator))
                    {
                        parts = Regex.Split(thisLine, keyValueSeparator);
                        if (parts[0].Equals("SYSTEM_ENCRYPTION_VALIDATOR") && parts[1].Equals("ENCRYPTION_OK"))
                        {
                            validated = true;
                            break; // TODO: might not be correct. Was : Exit While
                        }
                    }
                }

                // The file's empty.
            }
            catch (Exception) { }

            try
            {
                reader.Close();
            }
            catch (Exception) { }

            return(validated);
        }
コード例 #5
0
            public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                var modelName      = bindingContext.ModelName;
                var value          = bindingContext.ValueProvider.GetValue(modelName);
                var attemptedValue = value != null ? value.AttemptedValue : null;

                if (attemptedValue.IsNotNullOrEmpty())
                {
                    try
                    {
                        var decriptedData = EncryptionTools.Decrypt(
                            attemptedValue,
                            ConfigurationManager.AppSettings["EncryptKey"]);
                        var convertor = TypeDescriptor.GetConverter(bindingContext.ModelType);
                        return(convertor.ConvertFrom(decriptedData));
                    }
                    catch
                    {
                        return(bindingContext.ModelType.GetDefaultValue());
                    }
                }

                return(ModelBinders.Binders.DefaultBinder.BindModel(controllerContext, bindingContext));
            }
コード例 #6
0
        public bool GetValue(string theKey, ref string theValue, ref string errMsg)
        {
            if (!File.Exists(iniPath))
            {
                errMsg = "Ini file not found.";
                return(false);
            }

            if (useEncryption)
            {
                if (!ValidateEncryptedIniFile())
                {
                    errMsg = "This file is not an encrypted ini file created by this class, or decryption failed / invalid key.";
                    return(false);
                }
            }

            bool valueFound = false;

            try
            {
                StreamReader reader   = new StreamReader(iniPath);
                string       thisLine = "";
                String[]     parts;

                while (!(reader.Peek() == -1)) //Until eof
                {
                    thisLine = reader.ReadLine();

                    if (useEncryption)
                    {
                        thisLine = EncryptionTools.Decrypt(thisLine, encKey, true);
                        parts    = Regex.Split(thisLine, keyValueSeparator);

                        if (!(parts[0].Equals("SYSTEM_ENCRYPTION_VALIDATOR") && parts[1].Equals("ENCRYPTION_OK")))
                        {
                            if (parts[0].Equals(theKey))
                            {
                                theValue   = parts[1];
                                valueFound = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        parts = Regex.Split(thisLine, keyValueSeparator);
                        if (parts[0].Equals(theKey))
                        {
                            theValue   = parts[1];
                            valueFound = true;
                            break;
                        }
                    }
                }

                reader.Close();
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return(false);
            }

            if (valueFound)
            {
                return(true);
            }
            return(false);

            //ArrayList iniEntries = new ArrayList();
            //Int32 count = 0;
            //string[] parts = null;
            //string tmp = "";
            //string tmpKey = "";
            //string tmpValue = "";

            //// Get the contents of the ini file.
            //if (!ReadAllEntries(ref iniEntries, ref tmp))
            //{
            //    errMsg = tmp;
            //    return false;
            //}

            //// search for the key...
            //if (iniEntries.Count > 0)
            //{
            //    for (count = 0; count <= iniEntries.Count - 1; count++)
            //    {
            //        tmp = Convert.ToString(iniEntries[count]);
            //        parts = Regex.Split(tmp, keyValueSeparator);
            //        tmpKey = parts[0];
            //        tmpValue = parts[1];

            //        // Have we found it?
            //        if (theKey.ToLower().Trim() == tmpKey.ToLower().Trim())
            //        {
            //            theValue = tmpValue;
            //            return true;
            //        }
            //    }

            //    theValue = "";
            //}
            //else
            //{
            //    theValue = "No entries found in the ini file.";
            //}

            //return false;
        }