Google recaptcha enterprise:未找到您的默认凭据

问题描述 投票:0回答:1

我正在为我的网站 (.NET MVC) 设置 Google reCaptcha enterprise。它在我的开发环境中运行良好,但在生产服务器上找不到应用程序默认凭据。我收到的是此错误消息(在我的应用程序日志中):“未找到您的默认凭据”。

我在这里遵循 Google 的说明:https://cloud.google.com/docs/authentication/application-default-credentials#GAC

我已经尝试过以下两种方法:

  • 使用我的 .json 凭据文件的路径设置环境变量 GOOGLE_APPLICATION_CREDENTIALS。该文件存储在 IIS 虚拟目录之外的文件夹中。该文件夹具有对应用程序池标识的读取权限(为了更好的措施,我还尝试向每个人授予权限)。

  • 安装和配置 gcloud CLI,设置默认项目并运行

    gcloud auth application-default login
    命令。它已成功在 Appdata\Roaming\gcloud pplication_default_credentials.json 中创建凭据文件。

  • 当然,我尝试在设置环境变量后重新启动 IIS 和整个服务器(Win Server 2022)。

我的代码只是取自谷歌文档

private const string _key = "<mykey>";
private const string _projectID = "<myprojectid>";

//Just caching my client, as suggested by google
internal RecaptchaEnterpriseServiceClient _client
{
    get
    {
        if (HttpContext.Current.Session["RecaptchaClient"] != null)
        {
            return (RecaptchaEnterpriseServiceClient)HttpContext.Current.Session["RecaptchaClient"];
        }
        else
        {
            var client = RecaptchaEnterpriseServiceClient.Create();
            HttpContext.Current.Session["RecaptchaClient"] = client;
            return client;
        }
    }
}


public Assessment Evaluate(string token = "action-token", string recaptchaAction = "action-name")
{

    ProjectName projectName = new ProjectName(_projectID);

    // Build the assessment request.
    CreateAssessmentRequest createAssessmentRequest = new CreateAssessmentRequest()
    {
        Assessment = new Assessment()
        {
            // Set the properties of the event to be tracked.
            Event = new Event()
            {
                SiteKey = _key,
                Token = token,
                ExpectedAction = recaptchaAction
            },
        },
        ParentAsProjectName = projectName
    };

    Assessment response = _client.CreateAssessment(createAssessmentRequest);
    return response;
}

有人知道接下来要尝试什么吗?

.net google-cloud-platform gcloud recaptcha-v3 recaptcha-enterprise
1个回答
0
投票

尝试使用服务帐户进行身份验证作为替代方案。

在 GCP 中创建服务帐户并下载其 JSON 密钥文件。将 JSON 密钥文件放置在网站目录结构内的安全位置(为了安全起见,最好位于 Webroot 之外)。

然后更新您的代码以使用

GoogleCredential.FromServiceAccountKeyFile
从 JSON 密钥文件加载凭据,而不是依赖 ADC。

© www.soinside.com 2019 - 2024. All rights reserved.