Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

aspnet core: restrict middleware from swagger

$
0
0

HI, 

here is a  middle ware 

here the middleware gets executed first but , it directly return to response, instead of even  calling aspnet mvc. although i have it in pipeline, however is i remove the startup.cs entry of app.usemiddleware , it works perfectly, request comes to middlewar and then goes tomvc, executes filters pipeline and then comes to context ang everything works fine.. 

can you please let me know , if adding app.usemiddleware is some thing that causes shortcircuit ? 

2nd, i would like to add swagger to get api but  as middle ware which executes first. throws 404 from swagger . can we exclude swagger from catching up in between middle ware 


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?}");
});


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>