@@ -9,7 +9,7 @@ namespace AspNetCore.Honeypot;
99/// </summary>
1010class HoneypotService
1111{
12- public const string HttpContextItemName = "AspNetCore.Honeypot.IsHoneypotTrapped " ;
12+ public const string HttpContextItemName = "AspNetCore.Honeypot.IsHoneypotTriggered " ;
1313
1414 public HoneypotService ( IOptions < HoneypotOptions > options )
1515 {
@@ -22,42 +22,51 @@ public HoneypotService(IOptions<HoneypotOptions> options)
2222 private HoneypotOptions Options { get ; }
2323
2424 /// <summary>
25- /// IsTrapped
25+ /// Is honeypot triggered?
2626 /// </summary>
27- public bool IsTrapped ( HttpContext httpContext )
27+ public async Task < bool > IsTriggeredAsync ( HttpContext httpContext )
2828 {
2929 if ( httpContext . Items . TryGetValue ( HttpContextItemName , out object ? value ) == false )
3030 {
31- bool trapped = false ;
31+ bool triggered = false ;
3232
3333 if ( httpContext . Request . HasFormContentType == false )
3434 {
35- trapped = true ;
35+ triggered = true ;
3636 }
3737
38- if ( trapped == false && Options . IsFieldCheckEnabled )
38+ IFormCollection form = await httpContext . Request . ReadFormAsync ( ) ;
39+
40+ if ( triggered == false && Options . IsFieldCheckEnabled )
3941 {
4042 //check fields
41- trapped = httpContext . Request . Form . Any ( x => Options . IsFieldName ( x . Key ) && x . Value . Any ( v => string . IsNullOrEmpty ( v ) == false ) ) ;
43+ triggered = form . Any ( x => Options . IsFieldName ( x . Key ) && x . Value . Any ( v => string . IsNullOrEmpty ( v ) == false ) ) ;
4244 }
4345
44- if ( trapped == false && Options . IsTimeCheckEnabled )
46+ if ( triggered == false && Options . IsTimeCheckEnabled )
4547 {
4648 //check time
47- if ( httpContext . Request . Form . TryGetValue ( Options . TimeFieldName , out StringValues timeValues ) )
49+ if ( form . TryGetValue ( Options . TimeFieldName , out StringValues timeValues ) )
4850 {
49- if ( timeValues . Any ( ) )
51+ if ( timeValues . Count > 0 && timeValues [ 0 ] is string timeValue )
5052 {
51- TimeSpan diff = DateTime . UtcNow - new DateTime ( long . Parse ( timeValues . First ( ) ) , DateTimeKind . Utc ) ;
53+ if ( long . TryParse ( timeValue , out long time ) )
54+ {
55+ TimeSpan diff = DateTime . UtcNow - new DateTime ( time , DateTimeKind . Utc ) ;
5256
53- trapped = diff < Options . MinTimeDuration ;
57+ triggered = diff < Options . MinResponseTime ;
58+ }
59+ else
60+ {
61+ triggered = true ; //time field doesn't contain long value.
62+ }
5463 }
5564 }
5665 }
5766
58- httpContext . Items . Add ( HttpContextItemName , trapped ) ;
67+ httpContext . Items . Add ( HttpContextItemName , triggered ) ;
5968
60- return trapped ;
69+ return triggered ;
6170 }
6271 else
6372 {
0 commit comments