0

I'm trying to include Google ReCaptcha validation on my project, so in my C# class a have the following code doing the request to validate the key:

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)
{
    ...
}

But I keep getting the exception bellow. As I'm including the SecurityProtocol switch I didn't expect to receive this kind of error. Am I missing something here? My project is based on .NET framework 4.5. I already had this kind of error but it was resolved by including the SecurityProtocol switch, but not this time.

The underlying connection was closed: An unexpected error occurred on a send. System.IO.IOException: Authentication failed because the remote party has closed the transport stream. at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at 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, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.ConnectStream.WriteHeaders(Boolean async)

ahrlere
  • 1
  • 1
  • Possible duplicate of [Validating Recaptcha 2 (No CAPTCHA reCAPTCHA) in ASP.NET's server side](https://stackoverflow.com/questions/27764692/validating-recaptcha-2-no-captcha-recaptcha-in-asp-nets-server-side) – VDWWD Dec 10 '18 at 20:28

1 Answers1

0

Just resolved the issue by removing those lines.

ServicePointManager.ServerCertificateValidationCallback = delegate
{
    return true;
};
ahrlere
  • 1
  • 1