In the OWIN pipeline, i use a branch to configure a custom authentication middleware. How to return to pipeline root after branch executing?
app.Use<AuthenticationMiddleware1>();
app.Map("/branch", (application) => {
application.Use<AuthenticationMiddleware2>();
});
app.UseWebApi(new HttpConfiguration());
When i request http://server/branch
then web api is not configured and return 404
I tried to write a MapAndContinueMiddleware
:
public class MapAndContinueMiddleware:OwinMiddleware
{
public MapAndContinueMiddleware(OwinMiddleware next, MapOptions options) : base(next)
{
this.Options = options;
}
public MapOptions Options { get; }
public async override Task Invoke(IOwinContext context)
{
if(context.Request.Path.StartsWithSegments(this.Options.PathMatch))
{
await this.Options.Branch(context).ContinueWith((previousTask) =>
{
this.Next.Invoke(context);
});
}
else
{
await this.Next.Invoke(context);
}
}
}
with this extension :
public static IAppBuilder MapAndContinue(this IAppBuilder app, string pathMatch, Action<IAppBuilder> configuration)
{
// create branch and assign to options
IAppBuilder branch = app.New();
configuration(branch);
MapOptions options = new MapOptions {
PathMatch = new PathString(pathMatch),
Branch = (Func<IOwinContext, Task>)branch.Build(typeof(Func<IOwinContext, Task>))
};
return MapAndContinue(app, options);
}
public static IAppBuilder MapAndContinue(this IAppBuilder app, MapOptions options)
{
return app.Use<MapAndContinueMiddleware>(options);
}
But this has a strange behaviour : a web api request run the branch twice and doesn't return to client...!?