Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save divyang4481/14fe5d3324b416a8edaf51e9363cb901 to your computer and use it in GitHub Desktop.
Save divyang4481/14fe5d3324b416a8edaf51e9363cb901 to your computer and use it in GitHub Desktop.
CustomValidateAntiForgeryTokenAttribute
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
public class CustomValidateAntiForgeryTokenAttribute : ValidateAntiForgeryTokenAttribute
{
public override void OnAuthorization(AuthorizationFilterContext context)
{
// Inject custom logic before validation
// For example, you could log the request or perform some custom checks
CustomPreValidationLogic(context);
base.OnAuthorization(context);
// Inject custom logic after validation
// For example, you could log the result of the validation
CustomPostValidationLogic(context);
}
private void CustomPreValidationLogic(AuthorizationFilterContext context)
{
// Implement your custom pre-validation logic here
// Example: Logging
Console.WriteLine("Custom pre-validation logic executed");
}
private void CustomPostValidationLogic(AuthorizationFilterContext context)
{
// Implement your custom post-validation logic here
// Example: Logging
Console.WriteLine("Custom post-validation logic executed");
if (context.HttpContext.Response.StatusCode == (int)System.Net.HttpStatusCode.Forbidden)
{
// Handle validation failure
Console.WriteLine("Anti-forgery token validation failed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment