i have simple project ASP.Net Core with Razor. In this project I have single page that just open iframe. That works fine, but I want to know when iframe not loaded, is it possible?
@page
@using Microsoft.AspNetCore.Http.Extensions
@model IndexModel
<iframe src="@Model.BuildIFrameReference(HttpContext.Request.GetDisplayUrl())"
frameborder="0"
style="overflow: hidden;overflow-x: hidden;overflow-y: hidden;height: 100%;width: 100%;position: absolute;top: 0px;left: 0px;right: 0px;bottom: 0px"
height="100%"
width="100%"/>
So I choose different way to do this.I add new middleware that ping iframe url. If url for iframe returned not successed status code or have X-Frame-Options: Deny header I make redirect to default page.
public class CheckIframeUrlMiddleware
{
private readonly HttpClient _client;
private readonly RequestDelegate _next;
public CheckIframeUrlMiddleware(RequestDelegate next)
{
_next = next;
_client = new HttpClient();
}
public async Task InvokeAsync(HttpContext context, IframeReferenceBuilder _builder)
{
try
{
var response = await _client.GetAsync(_builder.BuildIFrameReference(context.Request.GetDisplayUrl()));
if (!response.IsSuccessStatusCode)
context.Response.Redirect(_builder.Settings.RedirectDomain);
if (response.Headers.TryGetValues("X-Frame-Options", out var values) && values.Contains("DENY"))
context.Response.Redirect(_builder.Settings.RedirectDomain);
await _next.Invoke(context);
}
catch
{
context.Response.Redirect(_builder.Settings.RedirectDomain);
}
}
}