Custom Authorized Filter with aspnet core

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

I found the following for Asp.net, however I was not able to port it to the asp.net core.

public class MyAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { if (httpContext.Request.Url.IsLoopback) { // It was a local request => authorize the guy return true; } return base.AuthorizeCore(httpContext); } } 

How can I port this to the asp.net core?

+6
source share
1 answer

You can create middleware in which you can automatically resolve requests coming from the local host.

 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 to the Configure launch method.

 app.UseMyAuthorize(); 

Asp.Net Core did not have the IsLoopback property. Here is the work for this fooobar.com/questions/556399 / ...

You can also read here Middleware here

+8
source

Source: https://habr.com/ru/post/1013445/


All Articles