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