このページは現在機能していません
HTTP ERROR 400
@page "/Button"
@model SimpleButton.ButtonModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Button1</title>
</head>
<body>
<div>Button Demo</div>
<form method="post">
<input type="submit" value="Button1" />
</form>
</body>
</html>
@addTagHelper
ディレクティブを追記して、タグヘルパーを有効にします。@page "/Button"
@model SimpleButton.ButtonModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Button1</title>
</head>
<body>
<div>Button Demo</div>
<form method="post">
<input type="submit" value="Button1" />
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Button1</title>
</head>
<body>
<div>Button Demo</div>
<form method="post">
<input type="submit" value="Button1" />
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Button1</title>
</head>
<body>
<div>Button Demo</div>
<form method="post">
<input type="submit" value="Button1" />
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8EL5y-BpEXFPhNBo25u1uGcl5v21mFISY00dX0plQh_JTMueLrfallMN8JixLkfqNaqlkHW2ZoaCg6iXe3dsXTrDoUxAQyY5zGxbsbdGAo_pa4HIPJK7FydXUnZ2mmsbATIQNf0xMh3IsSCmldj8Tig" /></form>
</body>
</html>
__RequestVerificationToken
のhidden フィールドが追加されていることがわかります。@Html.AntiForgeryToken()
を利用するとタグヘルパーを利用せずに、RequestVerificationTokenを送信できます。@page "/Button"
@model SimpleButtonBindProperty.ButtonModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Button</title>
</head>
<body>
<div>Button Demo</div>
<form method="post" asp-antiforgery="false">
Code:<input type="text" asp-for="data.Code"/><br/>
Name:<input type="text" asp-for="data.Name" /><br />
<input type="submit" value="Button1" />
</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 SimpleButtonBindProperty
{
public class ButtonModel : PageModel
{
[BindProperty]
public SimpleModel data { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
return RedirectToPage("/ButtonResult",new { name= data.Name, code=data.Code });
}
}
}
asp-antiforgery="false"
が指定されており、クロスサイト要求偽造が無効になっていますが、
ページモデル側では[IgnoreAntiforgeryToken]
属性が設定されておらず、POST時にRequestVerificationToken
が送信されず、
クロスサイト要求偽造チェックでNGとなり、400エラーが発生します。asp-antiforgery="false"
を削除し、クロスサイト要求偽造を有効にします。using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace SimpleButtonBindProperty
{
[IgnoreAntiforgeryToken]
public class ButtonModel : PageModel
{
[BindProperty]
public SimpleModel data { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
return RedirectToPage("/ButtonResult",new { name= data.Name, code=data.Code });
}
}
}
@Html.AntiForgeryToken()
を利用するとタグヘルパーを利用せずに、RequestVerificationTokenを送信できます。