1:asp-antiforgery="false" と 2:IgnoreAntiforgeryToken 属性の追加、両方を実施する必要があります。
片方だけの修正では、POST時にHTTP 400エラーが発生し正しく動作しません。
asp-antiforgery="false"
属性を記述します。 <form method="post" asp-antiforgery="false">
<!-- 中略 -->
</form>
[IgnoreAntiforgeryToken]
属性を記述します。namespace ExampleProject.Pages
{
[IgnoreAntiforgeryToken]
public class FormModel : PageModel
{
<!-- 中略 -->
}
}
@page
@model IgnoreAntiforgery.Pages.FormModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Form</title>
</head>
<body>
<p>クロスサイト要求偽造を無効にするデモ</p>
@{
if (Model.IsPost==true){
<p>ポストされました</p>
<p>ポストされた値:@Model.TextBoxValue</p>
}
}
<form method="post" asp-antiforgery="false">
<input type="text" asp-for="TextBoxValue"/>
<input type="submit" value="POST" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace IgnoreAntiforgery.Pages
{
[IgnoreAntiforgeryToken]
public class FormModel : PageModel
{
[BindProperty]
public string TextBoxValue{ get; set; }
public bool IsPost { get; set; }
public void OnGet()
{
IsPost = false;
}
public void OnPost()
{
IsPost = true;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace IgnoreAntiforgery
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
asp-antiforgery="false"
を記述します。 <form method="post" asp-antiforgery="false">
[IgnoreAntiforgeryToken]
属性を指定します。 [IgnoreAntiforgeryToken]
public class FormModel : PageModel
__RequestVerificationToken
フィールドが追加されていることが確認できます。<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Form</title>
</head>
<body>
<p>クロスサイト要求偽造を無効にするデモ</p>
<form method="post">
<input type="text" id="TextBoxValue" name="TextBoxValue" value="" />
<input type="submit" value="POST" />
<input name="__RequestVerificationToken" type="hidden"
value="CfDJ8Kcy693ModNFl4aCANCbqN3XfnUNZxpan6v2scEc66FWEl7Bq8vfrczwN_NdRPxRPmUjk60CXkSJVJ958cScidXUOQhgmCGJTvzAYguBDCHVRBM6wgMT6lqGSCVmDcXevhzMTIAA8B_Ve-O_XNAeUZc" /></form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Form</title>
</head>
<body>
<p>クロスサイト要求偽造を無効にするデモ</p>
<form method="post" action="/Form">
<input type="text" id="TextBoxValue" name="TextBoxValue" value="" />
<input type="submit" value="POST" />
</form>
</body>
</html>