如何删除 IdentityServer4 中的 X-Frame-Options header?

How Can I remove the X-Frame-Options header in IdentityServer4?

我在 .Net Core 3.0 中有一个 IdentityServer4 项目,我尝试删除 X-Frame-Options header 但失败了。我试过:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddAntiforgery(options => options.SuppressXFrameOptionsHeader = true);
    ...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    app.Use(async (context, next) =>
            {               
                context.Response.Headers.Remove("X-Frame-Options");
                await next();
            });
    ...
}

以上是因为我需要从 iframe 加载网页。 非常感谢你的帮助。 谢谢。

在 iframe 中托管登录屏幕确实是一种不好的做法,因为这样进行身份验证的用户不能 100% 确定他登录的是预期的 IdentityServer 而不是被黑的服务器。这一切都与信任有关,IdentitServer(或身份提供者)的 URL 是确保用户登录正确服务器的唯一方法。

如果您仍然需要删除它,请查看 IdentityServer 用户界面中的 SecurityHeadersAttribute.cs class。

我终于从 SecurityHeadersAttribute.cs 文件中删除了 header。我刚刚在同一个文件中添加了 frame-ancestors 内容安全策略,以启用从允许域上的 iframe 加载。