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); }
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); }
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); }
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); }
public static bool IsValidCaptcha(CaptchaVerificationData data, JObject settings) { var result = VerifyCaptcha(data, settings); return(result != null && JsonHelper.GetByPath <bool>(result, "verify.success")); }