大尺寸 (1MB) post 使用 FireFox + IIS + https + Windows 身份验证时 HTTP 错误 400
HTTP Error 400 when large size (1MB) post with FireFox + IIS + https + Windows Authentication
问题
我收到“HTTP 错误 400。请求动词无效。”当我在使用 ASP.NET 核心创建的应用程序中执行大尺寸 (1MB) post 时出错。
此问题仅在 Firefox 中出现,并且仅在启用 https 并使用 Windows 身份验证时出现。
我想知道如何解决这个大小限制。
发生条件
- 火狐
- IIS(带有 ASP.NET 核心应用程序)
- https
- Windows 身份验证
- 大尺寸 (1MB) post
我检查了什么
验证码
Form.cs
public class Form
{
public string Value { get; set; }
}
Controller.cs
public class HomeController : Controller
{
[Authorize]
[HttpGet]
public IActionResult Index(int? size)
{
return View(new Form { Value = new string('X', size ?? 1024 * 1024) });
}
[Authorize]
[HttpPost]
public IActionResult Index(Form form)
{
return View(form);
}
}
Index.cshtml
@model WebApplication.Models.Form
@{
ViewData["Title"] = "Home Page";
}
<div>@Model.Value.Length</div>
<form method="post" asp-antiforgery="false">
<input type="hidden" asp-for="Value"/>
<button type="submit">Post</button>
</form>
Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
浏览器差异
- 丁目 87.0
- IE 11
- FireFox 60.8 ESR
- FireFox 84.0.2
协议和身份验证文件
- FireFox 84.0.2 + https + 匿名身份验证
- FireFox 84.0.2 + http + Windows 身份验证(HTTP 错误 400。请求的大小 headers 太长。)
尺寸界限
- 1048569
- 1048570
“值=”长度 6 + 1048570 = 1MB。
不明点
- 错误400是服务器错误,但浏览器之间存在差异。
- 错误信息是“The request verb is invalid”,意思是“verb”是错误的。
- 服务器 header 是“Microsoft-HTTPAPI/2.0”,这是 http.sys 而不是 IIS 中的错误吗?
根据您的描述,我进行了测试,确实存在这个问题。
我在IIS日志中发现这个错误其实不是400错误而是401错误。也说明浏览器和服务器之间的验证有问题
您也可以在 IIS 中使用 FRT 来捕获此错误:
下面是使用 FRT 捕获的错误日志:
我们可以看到IIS中出现了401.2的错误。本质上,401.2 通常不是“错误”。这是身份验证的挑战。您的浏览器无法正确响应挑战。
问题
我收到“HTTP 错误 400。请求动词无效。”当我在使用 ASP.NET 核心创建的应用程序中执行大尺寸 (1MB) post 时出错。
此问题仅在 Firefox 中出现,并且仅在启用 https 并使用 Windows 身份验证时出现。
我想知道如何解决这个大小限制。
发生条件
- 火狐
- IIS(带有 ASP.NET 核心应用程序)
- https
- Windows 身份验证
- 大尺寸 (1MB) post
我检查了什么
验证码
Form.cs
public class Form
{
public string Value { get; set; }
}
Controller.cs
public class HomeController : Controller
{
[Authorize]
[HttpGet]
public IActionResult Index(int? size)
{
return View(new Form { Value = new string('X', size ?? 1024 * 1024) });
}
[Authorize]
[HttpPost]
public IActionResult Index(Form form)
{
return View(form);
}
}
Index.cshtml
@model WebApplication.Models.Form
@{
ViewData["Title"] = "Home Page";
}
<div>@Model.Value.Length</div>
<form method="post" asp-antiforgery="false">
<input type="hidden" asp-for="Value"/>
<button type="submit">Post</button>
</form>
Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
浏览器差异
- 丁目 87.0
- IE 11
- FireFox 60.8 ESR
- FireFox 84.0.2
协议和身份验证文件
- FireFox 84.0.2 + https + 匿名身份验证
- FireFox 84.0.2 + http + Windows 身份验证(HTTP 错误 400。请求的大小 headers 太长。)
尺寸界限
- 1048569
- 1048570
“值=”长度 6 + 1048570 = 1MB。
不明点
- 错误400是服务器错误,但浏览器之间存在差异。
- 错误信息是“The request verb is invalid”,意思是“verb”是错误的。
- 服务器 header 是“Microsoft-HTTPAPI/2.0”,这是 http.sys 而不是 IIS 中的错误吗?
根据您的描述,我进行了测试,确实存在这个问题。
我在IIS日志中发现这个错误其实不是400错误而是401错误。也说明浏览器和服务器之间的验证有问题
您也可以在 IIS 中使用 FRT 来捕获此错误:
下面是使用 FRT 捕获的错误日志:
我们可以看到IIS中出现了401.2的错误。本质上,401.2 通常不是“错误”。这是身份验证的挑战。您的浏览器无法正确响应挑战。