|
6 | 6 | using System.Text;
|
7 | 7 | using System.Text.Encodings.Web;
|
8 | 8 | using Microsoft.AspNetCore.Antiforgery;
|
| 9 | +using Microsoft.AspNetCore.Components.Endpoints.Forms; |
9 | 10 | using Microsoft.AspNetCore.Components.Endpoints.Rendering;
|
| 11 | +using Microsoft.AspNetCore.Components.Forms; |
10 | 12 | using Microsoft.AspNetCore.Components.Infrastructure;
|
11 | 13 | using Microsoft.AspNetCore.Diagnostics;
|
12 | 14 | using Microsoft.AspNetCore.Http;
|
@@ -76,14 +78,6 @@ private async Task RenderComponentCore(HttpContext context)
|
76 | 78 | return;
|
77 | 79 | }
|
78 | 80 |
|
79 |
| - context.Response.OnStarting(() => |
80 |
| - { |
81 |
| - // Generate the antiforgery tokens before we start streaming the response, as it needs |
82 |
| - // to set the cookie header. |
83 |
| - antiforgery!.GetAndStoreTokens(context); |
84 |
| - return Task.CompletedTask; |
85 |
| - }); |
86 |
| - |
87 | 81 | if (httpActivityContext != default)
|
88 | 82 | {
|
89 | 83 | _activityLinkStore.SetActivityContext(ComponentsActivityLinkStore.Http, httpActivityContext, null);
|
@@ -143,8 +137,17 @@ await _renderer.InitializeStandardComponentServicesAsync(
|
143 | 137 | var bufferingFeature = context.Features.GetRequiredFeature<IHttpResponseBodyFeature>();
|
144 | 138 | bufferingFeature.DisableBuffering();
|
145 | 139 |
|
| 140 | + // Store the tokens if not emitted already in case we stream a form in the response. |
| 141 | + antiforgery!.GetAndStoreTokens(context); |
| 142 | + |
146 | 143 | context.Response.Headers.ContentEncoding = "identity";
|
147 | 144 | }
|
| 145 | + else if (endpoint.Metadata.GetMetadata<ConfiguredRenderModesMetadata>()?.ConfiguredRenderModes.Length == 0) |
| 146 | + { |
| 147 | + // Disable token generation on EndpointAntiforgeryStateProvider if we are not streaming. |
| 148 | + var provider = (EndpointAntiforgeryStateProvider)context.RequestServices.GetRequiredService<AntiforgeryStateProvider>(); |
| 149 | + provider.DisableTokenGeneration(); |
| 150 | + } |
148 | 151 |
|
149 | 152 | // Importantly, we must not yield this thread (which holds exclusive access to the renderer sync context)
|
150 | 153 | // in between the first call to htmlContent.WriteTo and the point where we start listening for subsequent
|
|
0 commit comments