がDaniel`sの回答に基づいて、私は
[HttpPost]
[AllowAnonymous]
[IgnoreAntiforgeryToken]
public ActionResult Index()
{
if (!User.Identity.IsAuthenticated)
{
return NewIndex();
}
// rest of action
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult NewIndex()
{
// body of new action
}
にdocs draftに基づいて別のオプションを、コードを変更し、サービスとしてAntiforgery
を注入しています。
Project.json
"Microsoft.AspNetCore.Antiforgery": "1.0.0"
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IAntiforgery antiforgery)
{
...
public void ConfigureServices(IServiceCollection services)
{
services.AddAntiforgery();
...
からコントローラの検証。
public class MyController : Controller
{
private readonly IAntiforgery _antiforgery;
public AccountController(IAntiforgery antiforgery)
{
_antiforgery = antiforgery;
}
public ActionResult Index()
{
if (!User.Identity.IsAuthenticated)
{
await _antiforgery.ValidateRequestAsync(HttpContext);
}
// rest of action
}
}