Custom Authorize filter with aspnet core Custom Authorize filter with aspnet core asp.net asp.net

Custom Authorize filter with aspnet core


You can create a middleware in which you can authorize requests coming from localhost automatically.

public class MyAuthorize{   private readonly RequestDelegate _next;   public MyAuthorize(RequestDelegate next)   {      _next = next;   }   public async Task Invoke(HttpContext httpContext)   {     // authorize request source here.    await _next(httpContext);   }}

Then create an extension method

public static class CustomMiddleware{        public static IApplicationBuilder UseMyAuthorize(this IApplicationBuilder builder)        {            return builder.UseMiddleware<MyAuthorize>();        }}

and finally add it in startup Configure method.

app.UseMyAuthorize();

Asp.Net Core did not have IsLoopback property. Here is a work around for thishttps://stackoverflow.com/a/41242493/2337983

You can also read more about Middleware here