public class SecurityMiddleware
{
private readonly RequestDelegate _next;
public SecurityMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext)
{
//handle reuired function such
if(context.request.headers["mobile"] == "mobile")
{
//do something
}
else
{
//do something else
}
return _next(httpContext);
}
}
//extension method to add the middleware
public static class SecurityMiddlewareExtensions
{
public static IApplicationBuilder UseSecurityMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<SecurityMiddleware>();
}
}
now cinfigure this in startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseSecurityMiddleware();
app.UseSecurityMiddleware();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
this causes , a short circuit , if i rmove the app.use middleware(),. request pipeline works good, but if i add it, request comes and sends back directly whithout going to next one in pipeline.
if i remove app.usemiddleware , request pipeline starts form iddleware and ges to mvc,
could you please let me know if app.usemiddleware causes any shortcircuit >?
secondly i would like to use swagger, and would like to exempt from middleware. can we do that ?