PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 15, 2022

[FIXED] How to allow specific page in an iFrame - ASP.NET Core

 April 15, 2022     asp.net-core, c#, cross-domain, iframe     No comments   

Issue

We are working on a web project (ASP.NET Core 3.1). We have a requirement to allow to open only a specific page in an iframe from other origins. All other pages should not be accessible through iFrame.

We have tried a few ways as below:

  1. We have tried to remove X-Frame-Options: SAMEORIGIN from the header using middleware.
public async Task Invoke(HttpContext context)
{
    context.Response.OnStarting((state) =>
    {
        _headersToRemove.ForEach(header =>
        {
            if (context.Response.Headers.ContainsKey(header))
            {
                context.Response.Headers.Remove(header);
            }
        });

        return Task.FromResult(0);
    }, null);            

    await next.Invoke(context);
}

But, didn't get server headers (X-Frame-Options) here in middleware.

  1. We have used content security policy
<add name="Content-Security-Policy" value="frame-ancestors 'self' *.website.com" />

This works for the specified origin, however, it allows all the pages to be loaded in iframe.

  1. We have tried to remove X-Frame-Options header from the web.config file
<add name="X-Frame-Options" value="SAMEORIGIN" />
  1. We have tried to suppress X-Frame-Options header, but it allowed all the domains
public static void AddAntiForgery(this IServiceCollection services)
{
  services.AddAntiforgery(options =>
  {                
      options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;                
      options.Cookie.HttpOnly = true;
      options.Cookie.Name = "_app";
      options.Cookie.SameSite = SameSiteMode.Strict;
      options.SuppressXFrameOptionsHeader = true;
  });
}

So, the question is, how we can allow only a specific page in an iframe from other domains?

EDIT

I have already enabled the CORS.

<add name="Access-Control-Allow-Origin" value="*" />

Solution

We have tried several different ways and come up with a solution.

First, need to remove <add name="X-Frame-Options" value="SAMEORIGIN" /> from web config file.

Second, add X-Frame-Options programmatically for all the requests expect page that needs to open in iFrame.

app.UseWhen(context => !context.Request.Path.StartsWithSegments("/controller/action"), appBuilder =>
{
    appBuilder.Use(async (context, next) =>
    {
        context.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
        await next();
    });
});


Answered By - Divyang Desai
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing