如何在.cshtml页面上使用reCaptcha?

时间:2019-04-08 16:53:48

标签: c# razor recaptcha

我需要实现reCaptcha以便表单被机器人频繁使用和击中。问题是我没有文件,控制器,模型等背后的任何代码。我只有.cshtml页面可以使用。

如何使用剃刀在.cshtml页面上实现reCaptcha?

我已经研究了一段时间了,但是没有找到任何可行的解决方案。

*************************************************** *****更新******************************************** ******** 这是我在开发环境中使用.aspx.cs文件使用的代码。该代码在Submit_Click事件处理程序上调用。我以为可以在.cshtml页上的剃刀语法中使用此代码,但这是问题所在。

std::launder

我还需要创建此DataContract来处理JSON响应:

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

private void RunV2Call()
    {
        var sb2 = new StringBuilder();
        sb2.Append("https://www.google.com/recaptcha/api/siteverify?secret=");

        //----  OUR RECAPTCHA V2-CHECKBOX SECRET KEY
        var secretKey = "MySecretKey";
        sb2.Append(secretKey);

        //----  RESPONSE FROM RECAPTCHA CONTROL
        sb2.Append("&");
        sb2.Append("response=");
        var reCaptchaResponse = Request["g-recaptcha-response"];
        sb2.Append(reCaptchaResponse);

        //CLIENT IP ADDRESS
        //----  THIS IP ADDRESS PART IS OPTIONAL. 
        //----  IF YOU DO NOT WANT TO SEND IP ADDRESS YOU CAN
        //----  SKIP OR COMMENT OUT OR REMOVE BELOW 4 LINES
        //----  ALSO COMMENT OUT OR REMOVE THE METHOD 'GetUserIp()'
        sb2.Append("&");
        sb2.Append("remoteip=");
        var clientIpAddress = GetUserIp();
        sb2.Append(clientIpAddress);

        //----  MAKE THE API CALL AND DETERMINE VALIDITY
        using(var client = new WebClient())
        {
            var uri = sb2.ToString();
            var json = client.DownloadString(uri);
            var serializer = new DataContractJsonSerializer(typeof(RecaptchaApiResponse));
            var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
            var result = serializer.ReadObject(ms) as RecaptchaApiResponse;

            //--- CHECK IF WE ARE ABLE TO CALL API OR NOT
            if(result == null)
            {
                lblResult.Text = "";
                lblResult.Text = "Captcha was unable to make the api call";
            }
            else //---- IF YES
            {
                //----  IF API CALL CONTAINS ERRORS
                if(result.ErrorCodes != null)
                {
                    if(result.ErrorCodes.Count > 0)
                    {
                        foreach(var error in result.ErrorCodes)
                        {
                            lblResult.Text = "";
                            lblResult.Text = "reCAPTCHA Error: " + error;
                            lblResult.ForeColor = System.Drawing.Color.Red;
                        }
                    }
                }
                else //---- IF API DOES NOT CONTAIN ERRORS
                {
                    if(!result.Success) //----  IF CAPTCHA WAS UNSUCCESSFUL FOR SOME REASON
                    {
                        lblResult.Text = "";
                        lblResult.Text = "Captcha did not pass, please try again.";
                        lblResult.ForeColor = System.Drawing.Color.Red;
                    }
                    else //---- IF SUCCESSFULLY VERFIED; DO THE REST OF YOUR LOGIC
                    {
                        lblResult.Text = "";
                        lblResult.Text = "Success";
                        lblResult.ForeColor = System.Drawing.Color.Green;
                    }
                }
            }
        }
    }

private object GetUserIp()
    {
        var visitorsIpAddr = string.Empty;

        if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
        {
            visitorsIpAddr = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        }
        else if (!string.IsNullOrEmpty(Request.UserHostAddress))
        {
            visitorsIpAddr = Request.UserHostAddress;
        }

        return visitorsIpAddr;
    }

在我的Web窗体.aspx.cs文件中使用此代码,以及在.aspx页上如何包含reCaptcha的给定教程中,我没有遇到任何错误。那么,如何模仿.cshtml页面上的行为呢?请记住,文件后面没有任何控制器,模型或代码。我需要在.cshtml页面本身上执行此操作。我希望这足以继续下去。

***我遇到服务器运行时错误,页面无法加载。

0 个答案:

没有答案