using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
namespace Yavsc.Org.Tests;
///
/// Startup filter that injects after
/// the authentication and authorization middleware. The
/// contract wraps the existing
/// pipeline: the next delegate is the rest of the app's
/// pipeline, so we run our middleware before it but
/// after anything that was registered as a startup filter
/// earlier in the chain.
///
/// In practice this puts TestUserMiddleware ahead of
/// UseAuthentication (registered inside ConfigurePipeline)
/// because the production code path runs UseAuthentication
/// synchronously inside Configure, after all startup filters
/// have wrapped it. We want the opposite: the test identity must be
/// visible to authorization and the controller, so we register
/// TestUserMiddleware via
/// inside the filter such that it runs late in the chain. The
/// simplest way to achieve that is to register the middleware
/// after the production authorization pipeline: we wrap with our
/// middleware inside the filter, so our delegate sits between the
/// framework middleware (set up by Configure) and the rest of the
/// pipeline — meaning requests flow:
/// framework authN/authZ → TestUserMiddleware → next pipeline.
///
public class TestUserStartupFilter : IStartupFilter
{
public Action Configure(Action next)
{
return app =>
{
// Replay the production pipeline first (this is what
// Program.Main + ConfigurePipeline set up, including
// UseAuthentication and UseAuthorization).
next(app);
// Then add our middleware on top. UseMiddleware wires
// it through the same IMiddlewareActivator the framework
// uses, so the dependency on TestUserMiddleware is
// resolved from the request scope.
app.UseMiddleware();
};
}
}