【问题标题】:c# Authentication failed Google ReCaptchac# Authentication failed Google ReCaptcha
【发布时间】:2019-05-11 18:56:13
【问题描述】:

我正在尝试在我的项目中包含 Google ReCaptcha 验证,因此在我的 C# 类 a 中有以下代码执行验证密钥的请求:

try
{
    string Response = Request["g-recaptcha-response"];

    bool Valid = false;

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
    ServicePointManager.ServerCertificateValidationCallback = delegate
    {
        return true;
    };

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create
    ("https://www.google.com/recaptcha/api/siteverify?secret=MYKEY&response=" + Response);

    using (WebResponse wResponse = req.GetResponse())
    {

        using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream()))
        {
            string jsonResponse = readStream.ReadToEnd();

    ...
}
    }
}
catch (WebException ex)
{
    ...
}

但我不断收到以下异常。由于我包括了 SecurityProtocol 开关,我没想到会收到这种错误。我在这里错过了什么吗?我的项目基于 .NET 框架 4.5。我已经遇到过这种错误,但它通过包含 SecurityProtocol 开关得到解决,但这次没有。

底层连接已关闭:发送时发生意外错误。 System.IO.IOException:身份验证失败,因为远程方已关闭传输流。在 System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) 在 System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) 在 System.Net.Security.SslState。 CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) at System.Threading.ExecutionContext .RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback , 对象状态) 在 System.Net.TlsStream.ProcessAuthentication(LazyAs syncResult 结果)在 System.Net.TlsStream.Write(Byte[] 缓冲区,Int32 偏移量,Int32 大小)在 System.Net.ConnectStream.WriteHeaders(布尔异步)

【问题讨论】:

标签: c# asp.net ssl-certificate httpwebrequest httpwebresponse


【解决方案1】:

刚刚通过删除这些行解决了问题。

ServicePointManager.ServerCertificateValidationCallback = delegate
{
    return true;
};

【讨论】:

    猜你喜欢
    • 2022-12-27
    • 1970-01-01
    • 2020-05-16
    • 2022-01-19
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2023-03-06
    • 2018-01-01
    相关资源
    最近更新 更多