1

Google.Cloud.RecaptchaEnterprise ライブラリを使用して、クライアントが取得した新しいエンタープライズ キーのキャプチャ リクエストを検証しようとしています。

string _siteKey = ConfigurationManager.AppSettings["GoogleCaptcha.CheckboxCaptcha.SiteKey"];
string _apiKey = ConfigurationManager.AppSettings["GoogleCaptcha.ApiKey"];
string _projectId = ConfigurationManager.AppSettings["GoogleCaptcha.ProjectId"];
string recaptchaAction = "CreateAccountAssessment";
try {
    var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
    string credential_path = appPath + "googlecredentials.json";
    System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);

    RecaptchaEnterpriseServiceClient client =
    RecaptchaEnterpriseServiceClient.Create();

    CreateAssessmentRequest createAssessmentRequest = new CreateAssessmentRequest()
    {
        Assessment = new Assessment()
        {
            Event = new Event()
            {
                SiteKey = _siteKey,
                Token = formResponse,
                ExpectedAction = "Create_Account"
            },
            Name = recaptchaAction,                         
        },
        Parent = _projectId
    };

    Assessment response = client.CreateAssessment(createAssessmentRequest);

    if (response.TokenProperties.Valid == false)
    {
        Sitecore.Diagnostics.Log.Error("The CreateAssessment() call failed " +
            "because the token was invalid for the following reason: " +
            response.TokenProperties.InvalidReason.ToString(), this);

        return "Invalid captcha.";
    }
    else
    {
        if (response.Event.ExpectedAction == recaptchaAction)
        {
            Sitecore.Diagnostics.Log.Error("The reCAPTCHA score for this token is: " +
                response.RiskAnalysis.Score.ToString(), this);

            return "";
        }
        else
        {
            Sitecore.Diagnostics.Log.Error("The action attribute in your reCAPTCHA " +
                "tag does not match the action you are expecting to score", this);

            return "Invalid captcha.";

        }

    }
}
catch (Exception ex)
{
    Sitecore.Diagnostics.Log.Error("Error validating captcha on " + _url + "; " + ex.Message, this);
    return "Unable to connect to captcha service.";
}

すべてのプロパティが正しいことがわかる限り、次のエラーがスローされますAssessment response = client.CreateAssessment(createAssessmentRequest);

Status(StatusCode="InvalidArgument", Detail="リクエストに無効な引数が含まれています。", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1621287236.280000000","description":"ピア ipv6 から受信したエラー:[2607:f8b0:4006:81a::200a]:443","file":"T:\src\github\grpc\workspace_csharp_ext_windows_x64\src\core\lib\surface\call.cc","file_line": 1062,"grpc_message":"リクエストに無効な引数が含まれています。","grpc_status":3}")

4

1 に答える 1