/// <summary> /// /// </summary> /// <returns></returns> public bool HasRequestValidCaptchaEntry( Language captchaGeneratorLanguage, DisplayMode captchaGeneratorDisplayMode, DNTCaptchaBase model = null) { var httpContext = _contextAccessor.HttpContext; if (!shouldValidate(httpContext)) { _logger.LogDebug($"Ignoring ValidateDNTCaptcha during `{httpContext.Request.Method}`."); return(true); } var(captchaText, inputText, cookieToken) = getFormValues(httpContext, model); if (string.IsNullOrEmpty(captchaText)) { _logger.LogDebug("CaptchaHiddenInput is empty."); return(false); } if (string.IsNullOrEmpty(inputText)) { _logger.LogDebug("CaptchaInput is empty."); return(false); } inputText = inputText.ToEnglishNumbers(); if (!long.TryParse( inputText, NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out long inputNumber)) { _logger.LogDebug("inputText is not a number."); return(false); } var decryptedText = _captchaProtectionProvider.Decrypt(captchaText); var numberToText = _captchaTextProvider(captchaGeneratorDisplayMode).GetText(inputNumber, captchaGeneratorLanguage); if (decryptedText?.Equals(numberToText) != true) { _logger.LogDebug($"{decryptedText} != {numberToText}"); return(false); } if (!isValidCookie(httpContext, decryptedText, cookieToken)) { return(false); } return(true); }
/// <summary> /// Gets the value associated with the specified token. /// </summary> /// <param name="context"></param> /// <param name="token">The specified token.</param> public string?GetValue(HttpContext context, string token) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (!context.Request.Cookies.TryGetValue(token, out var cookieValue)) { _logger.LogDebug("Couldn't find the captcha cookie in the request."); return(null); } Remove(context, token); if (string.IsNullOrWhiteSpace(cookieValue)) { _logger.LogDebug("Couldn't find the captcha cookie's value in the request."); return(null); } var decryptedValue = _captchaProtectionProvider.Decrypt(cookieValue); return(decryptedValue?.Replace(context.GetSalt(_captchaProtectionProvider), string.Empty, StringComparison.Ordinal)); }
/// <summary> /// Gets the value associated with the specified token. /// </summary> /// <param name="context"></param> /// <param name="token">The specified token.</param> public string GetValue(HttpContext context, string token) { if (!_memoryCache.TryGetValue(token, out string cookieValue)) { _logger.LogDebug("Couldn't find the captcha cookie in the request."); return(null); } _memoryCache.Remove(token); var decryptedValue = _captchaProtectionProvider.Decrypt(cookieValue); return(decryptedValue?.Replace(context.GetSalt(_captchaProtectionProvider), string.Empty)); }
/// <summary> /// Gets the value associated with the specified token. /// </summary> /// <param name="context"></param> /// <param name="token">The specified token.</param> public string?GetValue(HttpContext context, string token) { var cookieValueBytes = _distributedCache.Get(token); if (cookieValueBytes == null) { _logger.LogDebug("Couldn't find the captcha cookie in the request."); return(null); } _distributedCache.Remove(token); var decryptedValue = _captchaProtectionProvider.Decrypt(Encoding.UTF8.GetString(cookieValueBytes)); return(decryptedValue?.Replace(context.GetSalt(_captchaProtectionProvider), string.Empty, StringComparison.Ordinal)); }
/// <summary> /// Gets the value associated with the specified token. /// </summary> /// <param name="context"></param> /// <param name="token">The specified token.</param> public string GetValue(HttpContext context, string token) { var value = context.Session.GetString(token); if (string.IsNullOrWhiteSpace(value)) { _logger.LogDebug("Couldn't find the captcha's session value in the request."); return(null); } Remove(context, token); var decryptedValue = _captchaProtectionProvider.Decrypt(value); return(decryptedValue?.Replace(context.GetSalt(_captchaProtectionProvider), string.Empty)); }
public IActionResult Refresh(string data) { if (string.IsNullOrWhiteSpace(data)) { return(BadRequest()); } var decryptedModel = _captchaProtectionProvider.Decrypt(data); if (decryptedModel == null) { return(BadRequest()); } var model = _serializationProvider.Deserialize <DNTCaptchaTagHelperHtmlAttributes>(decryptedModel); if (model == null) { return(BadRequest()); } invalidateToken(model); var tagHelper = HttpContext.RequestServices.GetRequiredService <DNTCaptchaTagHelper>(); tagHelper.BackColor = model.BackColor; tagHelper.FontName = model.FontName; tagHelper.FontSize = model.FontSize; tagHelper.ForeColor = model.ForeColor; tagHelper.Language = model.Language; tagHelper.Max = model.Max; tagHelper.Min = model.Min; tagHelper.Placeholder = model.Placeholder; tagHelper.TextBoxClass = model.TextBoxClass; tagHelper.TextBoxTemplate = model.TextBoxTemplate; tagHelper.ValidationErrorMessage = model.ValidationErrorMessage; tagHelper.ValidationMessageClass = model.ValidationMessageClass; tagHelper.RefreshButtonClass = model.RefreshButtonClass; tagHelper.DisplayMode = model.DisplayMode; tagHelper.UseRelativeUrls = model.UseRelativeUrls; tagHelper.UseNoise = model.UseNoise; var tagHelperContext = new TagHelperContext( allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object> { { typeof(IUrlHelper), this.Url } }, uniqueId: Guid.NewGuid().ToString("N")); var tagHelperOutput = new TagHelperOutput( tagName: "div", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent(string.Empty); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); tagHelper.ViewContext = ViewContext ?? new ViewContext( new ActionContext(this.HttpContext, HttpContext.GetRouteData(), ControllerContext.ActionDescriptor), new FakeView(), new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = null }, new TempDataDictionary(this.HttpContext, _tempDataProvider), TextWriter.Null, new HtmlHelperOptions()); tagHelper.Process(tagHelperContext, tagHelperOutput); var attrs = new StringBuilder(); foreach (var attr in tagHelperOutput.Attributes) { attrs.Append(' ').Append(attr.Name).Append("='").Append(attr.Value).Append('\''); } var content = $"<div {attrs}>{tagHelperOutput.Content.GetContent()}</div>"; return(Content(content)); }