Created
June 11, 2024 03:46
-
-
Save divyang4481/14fe5d3324b416a8edaf51e9363cb901 to your computer and use it in GitHub Desktop.
CustomValidateAntiForgeryTokenAttribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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