VahidN / DNTCaptcha.Core

DNTCaptcha.Core is a captcha generator and validator for ASP.NET Core applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

پشتیبانی از post به صورت json

parsintalk opened this issue · comments

سلام و عرض ادب،
ضمن تشکر از زحمات.
عذر می خوام پروژه شما از حالت ارسال داده به صورت json پشتیبانی نمی کنه؟
من یه تیکه کد به سورس شما اضافه کردم که این کارو انجام بده، امکانش هست شما اگر صلاح می دونید به شکل صحیح به پروژتون اضافه کنید؟

ValidateDNTCaptchaAttribute.cs, line 34:

if(context != null)
            context.HttpContext.Request.EnableBuffering();

DNTCaptchaValidatorService.cs, line 131:

private static string getJsonValue(string text, string name)
 {
#pragma warning disable MA0009 // Add regex evaluation timeout
            var math = System.Text.RegularExpressions.Regex.Match(text, "(?:\"" + name +"\":\")(.*?)(?:\")");
#pragma warning restore MA0009 // Add regex evaluation timeout

            if (math.Groups?.Count > 0)
            {
                return math.Groups[1].Value;
            }

            return "";
        }

        private static string readRequestBody(HttpContext context)
        {
            var body = "";

            // Leave the body open so the next middleware can read it.
            using (var reader = new System.IO.StreamReader(
                context.Request.Body,
                encoding: System.Text.Encoding.UTF8,
                detectEncodingFromByteOrderMarks: false,
                leaveOpen: true))
            {
                body = reader.ReadToEnd();

                // Reset the request body stream position so the next middleware can read it
                context.Request.Body.Position = 0;
            }

            return body;
        }

و داخل getFormValues به این روش بدون استفاده از json parser، با regex داده ها رو بدست آوردم:

var body = readRequestBody(httpContext);
captchaTextForm = getJsonValue(body, captchaHiddenInputName);
inputTextForm = getJsonValue(body, captchaInputName);
cookieTokenForm = getJsonValue(body, captchaHiddenTokenName);

مشکلم حل شد و داره کار می کنه.

سپاس فراوان.