@@ -10,10 +10,12 @@ namespace Microsoft.AspNetCore.Components.Endpoints.Forms;
1010internal class EndpointAntiforgeryStateProvider ( IAntiforgery antiforgery ) : DefaultAntiforgeryStateProvider ( )
1111{
1212 private HttpContext ? _context ;
13+ private bool _canGenerateToken ;
1314
1415 internal void SetRequestContext ( HttpContext context )
1516 {
1617 _context = context ;
18+ _canGenerateToken = true ;
1719 }
1820
1921 public override AntiforgeryRequestToken ? GetAntiforgeryToken ( )
@@ -24,17 +26,23 @@ internal void SetRequestContext(HttpContext context)
2426 return _currentToken ;
2527 }
2628
27- // We already have a callback setup to generate the token when the response starts if needed.
28- // If we need the tokens before we start streaming the response, we'll generate and store them;
29- // otherwise we'll just retrieve them.
30- // In case there are no tokens available, we are going to return null and no-op.
31- var tokens = ! _context . Response . HasStarted ? antiforgery . GetAndStoreTokens ( _context ) : antiforgery . GetTokens ( _context ) ;
32- if ( tokens . RequestToken is null )
29+ if ( _currentToken == null && _canGenerateToken )
3330 {
34- return null ;
31+ // We already have a callback setup to generate the token when the response starts if needed.
32+ // If we need the tokens before we start streaming the response, we'll generate and store them;
33+ // otherwise we'll just retrieve them.
34+ // In case there are no tokens available, we are going to return null and no-op.
35+ var tokens = ! _context . Response . HasStarted ? antiforgery . GetAndStoreTokens ( _context ) : antiforgery . GetTokens ( _context ) ;
36+ if ( tokens . RequestToken is null )
37+ {
38+ return null ;
39+ }
40+
41+ _currentToken = new AntiforgeryRequestToken ( tokens . RequestToken , tokens . FormFieldName ) ;
3542 }
3643
37- _currentToken = new AntiforgeryRequestToken ( tokens . RequestToken , tokens . FormFieldName ) ;
3844 return _currentToken ;
3945 }
46+
47+ internal void DisableTokenGeneration ( ) => _canGenerateToken = false ;
4048}
0 commit comments