3

I have created an action filter for HTTP request validation in ASP.NET Core 3.1 Web API.

Unfortunately it is not working. The problem has to be at controller level, because the same works in .NET Framework.

CustomAuthorize.cs

public class CustomAuthorize : ActionFilterAttribute
{
    private readonly IConfiguration _configuration;

    public CustomAuthorize(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public override void OnActionExecuting(HttpActionContext context)
    {
        string secrect = _configuration.GetSection("AuthJWT").GetSection("Secrect").Value;
        var request = context.Request.Headers.GetValues("AuthJWT").FirstOrDefault();
        //Add Validation code here
        base.OnActionExecuting(context);

    }
}

HomeController

[Route("[controller]")]
[ApiController]
[ServiceFilter(typeof(CustomAuthorize))]
public class HomeController : ControllerBase
{       
    [HttpGet("getdata")]
    public IActionResult GetData()
    {
        return Ok("Success");
    }
}

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors();

        services.AddControllers();

        services.AddScoped<CustomAuthorize>();

    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
2
  • we need to see the complete code for the filter and the validation code at least the relevant portion.... Commented Jul 23, 2020 at 16:02
  • also how is it "not working"... we need to be able to reproduce this. so you need to post enough code and information for a fully reproducible example Commented Jul 23, 2020 at 16:03

1 Answer 1

3

ActionFilterAttribute abstract class includes the following methods to override:

void OnActionExecuted(ActionExecutedContext filterContext)
void OnActionExecuting(ActionExecutingContext filterContext)
void OnResultExecuted(ResultExecutedContext filterContext)
void OnResultExecuting(ResultExecutingContext filterContext)

Use ActionExecutingContext in OnActionExecuting.

Only need to modify OnActionExecuting of CustomAuthorize. Other codes are same as u.

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            string secrect = _configuration.GetSection("AuthJWT").GetSection("Secrect").Value;
            
            var request = context.HttpContext.Request.Headers["AuthJWT"].FirstOrDefault();
            //Add Validation code here
            base.OnActionExecuting(context);

        }

Test of result

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.