Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
When I call an api from a blazor page I think my middleware crash and I don't understand why
<button class="btn btn-primary" @onclick="SayHello">SayHello</button>
<p role="status">Say: @say</p>
@code {
[CascadingParameter]
private Task<AuthenticationState>? authenticationState { get; set; }
private string say = string.Empty;
private async Task SayHello()
{
var client = Factory.CreateClient("WebApp");
say = await client.GetStringAsync("/Hello");
}
protected override async Task OnInitializedAsync()
{
if (authenticationState is not null)
{
var isAuthenticated = (await authenticationState).User.Identity?.IsAuthenticated;
if (isAuthenticated == true)
{
var client = Factory.CreateClient("WebApp");
var test = await client.GetStringAsync("/Hello");
}
}
}
}
If the api call is called inside OnInitializedAsync it doesn't work, when it's trigger by the Hello button outside of OnInitializedAsync it works.
I wait the authenticatioState correctly I think.
It crash server side in my user middleware, when I set my user
public class UserServiceMiddleware(RequestDelegate next)
{
private readonly RequestDelegate next = next ?? throw new ArgumentNullException(nameof(next));
public async Task InvokeAsync(HttpContext context, UserService service)
{
service.SetUser(context.User);
await next(context);
}
}
(like explained here https://learn.microsoft.com/en-us/aspnet/core/blazor/security/server/additional-scenarios?view=aspnetcore-8.0) to be able to inject my user in other services server side.
Very strange.
Expected Behavior
Same behavior as calling the API with a button click... I'dont understand why my middleware creates this issue for OnInitializedAsync. It crash on await next(context);
Steps To Reproduce
No response
Exceptions (if any)
it returns
HttpRequestException: Response status code does not indicate success: 302 (Found).
System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
System.Net.Http.HttpClient.GetStringAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
Ubik.Accounting.WebApp.Client.Pages.Auth.OnInitializedAsync() in Auth.razor
var test = await client.GetStringAsync("/Hello");
Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
Microsoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.WaitForResultReady(bool waitForQuiescence, PrerenderedComponentHtmlContent result)
Microsoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.RenderEndpointComponent(HttpContext httpContext, Type rootComponentType, ParameterView parameters, bool waitForQuiescence)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContext context)
Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContext context)
Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext+<>c+<<InvokeAsync>b__10_0>d.MoveNext()
Ubik.Accounting.WebApp.Security.UserServiceMiddleware.InvokeAsync(HttpContext context, UserService service) in UserServiceMiddleware.cs
await next(context);
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
.NET Version
8.0
Anything else?
No response