示例#1
0
        public static JObject InjectInfo(JObject settings, string path, string key, object value)
        {
            var container = JsonHelper.GetByPath <JObject>(settings, path);

            if (container == null)
            {
                container = new JObject();
                JsonHelper.SetByPath(settings, path, container);
            }

            JsonHelper.AddOrSet(container, key, value);

            return(container);
        }
示例#2
0
        public static R FindAttributeValue <R>(JToken token, string key, R defaultValue = default)
        {
            JToken attribute = null;

            if (token.Type == JTokenType.Property)
            {
                attribute = JsonHelper.GetByPath((token as JProperty).Value, key);
            }
            else
            {
                attribute = JsonHelper.GetByPath(token, key);
            }

            return(attribute != null?attribute.Value <R>() : defaultValue);
        }
示例#3
0
        public static T GetSettingsFor <T>(JObject settings, string entryPath) where T : SettingsEntry
        {
            T entry = default;

            try
            {
                if (settings != null)
                {
                    var jsonEntry = JsonHelper.GetByPath(settings, entryPath);
                    if (jsonEntry != null)
                    {
                        entry = JsonConvert.DeserializeObject <T>(jsonEntry.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error on GetSettingsFor settings:{@settings} entryPath:{entryPath}", settings, entryPath);
            }

            return(entry);
        }
示例#4
0
        public static JObject VerifyCaptcha(CaptchaVerificationData data, JObject settings)
        {
            Log.Debug("SecurityHelper:VerifyCaptcha: {data}", JsonConvert.SerializeObject(data, Formatting.Indented));
            var result = new JObject();

            try
            {
                var captcha      = JsonHelper.FindTokenValue <JObject>(settings, "captcha");
                var verifyUrl    = JsonHelper.GetByPath <string>(captcha, "server.verifyUrl");
                var secret       = JsonHelper.GetByPath <string>(captcha, "server.secret");
                var verifyParams = $"secret={secret}&response={data.Token}";
                if (!string.IsNullOrEmpty(data.Ip))
                {
                    verifyParams += $"&remoteip={data.Ip}";
                }

                using (var client = new WebClient())
                {
                    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                    var response = client.UploadString(verifyUrl, verifyParams);
                    if (!string.IsNullOrEmpty(response) && response.Contains("{"))
                    {
                        result.Add("verify", JsonConvert.DeserializeObject <JToken>(response));
                    }
                    else
                    {
                        result.Add("verify", response);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Could not verify captcha");
            }

            return(result);
        }
示例#5
0
        public static bool IsValidCaptcha(CaptchaVerificationData data, JObject settings)
        {
            var result = VerifyCaptcha(data, settings);

            return(result != null && JsonHelper.GetByPath <bool>(result, "verify.success"));
        }