@@ -11,7 +11,7 @@ public class CaptchaService(IHttpClientFactory clientFactory, IOptions<CaptchaOp
1111
1212 // Verify captcha. Optionally add overload to pass in remoteIp as in the docs
1313 // https://docs.hcaptcha.com/#verify-the-user-response-server-side
14- public async Task < HCaptchaResult ? > VerifyAsync ( string secret , string response , string sitekey )
14+ public async Task < HCaptchaResult ? > VerifyAsync ( string secret , string response , string sitekey , CancellationToken cancellationToken = default )
1515 {
1616 // create post data
1717 List < KeyValuePair < string , string > > postData =
@@ -21,10 +21,10 @@ public class CaptchaService(IHttpClientFactory clientFactory, IOptions<CaptchaOp
2121 new KeyValuePair < string , string > ( "sitekey" , sitekey )
2222 ] ;
2323
24- return await PostVerification ( postData ) ;
24+ return await PostVerification ( postData , cancellationToken ) ;
2525 }
2626
27- public async Task < HCaptchaResult ? > VerifyAsync ( string ? response )
27+ public async Task < HCaptchaResult ? > VerifyAsync ( string ? response , CancellationToken cancellationToken = default )
2828 {
2929 if ( string . IsNullOrWhiteSpace ( response ) )
3030 {
@@ -33,21 +33,21 @@ public class CaptchaService(IHttpClientFactory clientFactory, IOptions<CaptchaOp
3333 string secret = Options . SecretKey ?? throw new InvalidOperationException ( $ "{ CaptchaOptions . CaptchaSender } { nameof ( Options . SecretKey ) } is unexpectedly null") ;
3434 string sitekey = Options . SiteKey ?? throw new InvalidOperationException ( $ "{ CaptchaOptions . CaptchaSender } { nameof ( Options . SiteKey ) } is unexpectedly null") ;
3535
36- return await VerifyAsync ( secret , response , sitekey ) ;
36+ return await VerifyAsync ( secret , response , sitekey , cancellationToken ) ;
3737 }
3838
39- public async Task < HCaptchaResult ? > PostVerification ( List < KeyValuePair < string , string > > postData )
39+ public async Task < HCaptchaResult ? > PostVerification ( List < KeyValuePair < string , string > > postData , CancellationToken cancellationToken = default )
4040 {
4141 HttpClient client = ClientFactory . CreateClient ( "hCaptcha" ) ;
4242
4343 // request api
4444 HttpResponseMessage res = await client . PostAsync (
4545 // base url is given in IHttpClientFactory service registration
4646 // hCaptcha wants URL-encoded POST
47- "/siteverify" , new FormUrlEncodedContent ( postData ) ) ;
47+ "/siteverify" , new FormUrlEncodedContent ( postData ) , cancellationToken ) ;
4848
4949 res . EnsureSuccessStatusCode ( ) ;
5050 // convert JSON string into Class
51- return JsonSerializer . Deserialize < HCaptchaResult > ( await res . Content . ReadAsStringAsync ( ) ) ;
51+ return JsonSerializer . Deserialize < HCaptchaResult > ( await res . Content . ReadAsStringAsync ( cancellationToken ) ) ;
5252 }
5353}
0 commit comments