asp.net-core - 从 Iframe POST 表单时,Asp razor 页面返回 400 代码

标签 asp.net-core razor-pages

我正在尝试从 iframe 发布表单。但是我收到 400 错误,我不知道为什么。在 iframe 中,它显示了 GET 请求,我还在 GET 和 POST 方法 header 中设置了 "X-Frame-Options", "ALLOW-FROM ...

这是请求:

Host: localhost:52136
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://localhost:52136/
Content-Type: application/x-www-form-urlencoded
Content-Length: 796
Connection: keep-alive
Upgrade-Insecure-Requests: 1

然后回应:

HTTP/1.1 400 Bad Request
Server: Kestrel
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcR2VydmlsIERvdWJhbFxzb3VyY2VccmVwb3NcQWNvbXBhIGNoYXJ0XE9ubGluZUFnZW5jeQ==?=
X-Powered-By: ASP.NET
Date: Tue, 18 Dec 2018 20:41:55 GMT
Content-Length: 0

当我直接从浏览器中的同一个 url 提交同一个表单时,我没有收到任何错误。

如何在 iframe 中修复它?

最佳答案

我在 SharePoint Online 网站的 Iframe 中使用 Asp.net 核心应用程序(Razor Pages)时遇到了同样的错误。

解决方案:

在您的 asp.net 核心应用程序的 Startup.cs 文件中添加以下配置。 (您可能需要添加 Microsoft.AspNetCore.Mvc 的引用)

第 1 步

启用 Content Security Policy将父 URL 添加到受信任的 CORS 域。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.Use(async (context, next) =>    
    {
        context.Response.Headers.Add("Content-Security-Policy", "frame-ancestors https://mysharepointparentsite.sharepoint.com/");
        await next();
    });
}

第 2 步 启用 Ignore Anti Forgery Token验证。

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddRazorPagesOptions(_options =>
    {
        _options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
    });
}

关于asp.net-core - 从 Iframe POST 表单时,Asp razor 页面返回 400 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53840958/

相关文章:

.net - 从 VS 2017 .NET Core 项目中的发布目录中排除文件

c# - 如何将 Identity Server 与 .NET Core 2.1 一起使用?

asp.net-core - 为什么 blazor dotnet core 3.1 webassembly 客户端和共享项目是 netstandard 2.1?

c# - 运行 MSBuild 时发生内部故障 - 无法加载文件或程序集 System.Net.Primitives

asp.net-core - aspnet-codegenerator : No code generators available,即使添加了Microsoft.VisualStudio.Web.CodeGeneration.Design

c# - Razor 页面路由与 Web API 中的方式相同

asp.net-core - asp.net core 中的计划任务的 IHostedService 和 Task 有什么区别?

c# - 返回 View (型号 : MyModel); equivalent in ASP.Net Core Razor Pages

asp.net-core - Fluent 验证在 Bootstrap 4 Modal 中不起作用

c# - 为什么我的绑定(bind) List<T> 使用 foreach 循环返回空以在 razor 页面上显示值?