Custom Authorize filter with aspnet core

前端 未结 1 954
轮回少年
轮回少年 2021-02-06 08:48

Hi I am trying to create a custom authorize filter that will allow me to authorize requests coming from localhost automatically (which will be used for my tests).

I foun

1条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 09:31

    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();
            }
    }
    

    and finally add it in startup Configure method.

    app.UseMyAuthorize();
    

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

    You can also read more about Middleware here

    0 讨论(0)
提交回复
热议问题