I\'m using IdentityServer4 with ASP.NET Core 2.2. On the Post Login method I have applied the ValidateAntiForgeryToken. Generally after 20 minutes to 2 hours of sitting on the l
This was my final solution. I added a attribute using the IAntifogery dependency injection.
public class CustomValidationAttribute : ActionFilterAttribute
{
private IAntiforgery _antiForgery { get; }
public CustomValidationAttribute(IAntiforgery antiforgery)
{
_antiForgery = antiforgery;
}
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var isRequestValid = await this._antiForgery.IsRequestValidAsync(context.HttpContext);
if (!isRequestValid)
{
//Add Code here if token is not valid
return;
}
await next();
}
}
Add the attribute to your controller methods that also use [HttpPost]
[TypeFilter(typeof(CustomValidationAttribute))]