.NET (ASP.NET Core)
using System.Net.Http.Headers;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
public class DisposableEmailFilter : IAsyncActionFilter
{
private readonly IHttpClientFactory _httpFactory;
private readonly IConfiguration _config;
public DisposableEmailFilter(IHttpClientFactory httpFactory, IConfiguration config)
{
_httpFactory = httpFactory;
_config = config;
}
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var email = context.ActionArguments.Values
.OfType<string>().FirstOrDefault(v => v.Contains("@"));
if (email != null)
{
try
{
var client = _httpFactory.CreateClient();
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", _config["DG_KEY"]);
var response = await client.GetAsync(
$"https://api.disposableguard.com/v1/check?email={Uri.EscapeDataString(email)}");
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
var data = JsonDocument.Parse(json).RootElement;
if (data.GetProperty("is_disposable").GetBoolean())
{
context.Result = new BadRequestObjectResult(
new { error = "Please use a real email address." });
return;
}
}
}
catch { /* fail-open */ }
}
await next();
}
}Notes
Register with `builder.Services.AddScoped<DisposableEmailFilter>()` and apply as a service filter or manually on controllers.