<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
	<id>https://monobook.org/w/index.php?action=history&amp;feed=atom&amp;title=ASP.NET_Core%E3%81%A7JavaScript%E3%81%AEfetch%E9%96%A2%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%86</id>
	<title>ASP.NET CoreでJavaScriptのfetch関数を使う - 版の履歴</title>
	<link rel="self" type="application/atom+xml" href="https://monobook.org/w/index.php?action=history&amp;feed=atom&amp;title=ASP.NET_Core%E3%81%A7JavaScript%E3%81%AEfetch%E9%96%A2%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%86"/>
	<link rel="alternate" type="text/html" href="https://monobook.org/w/index.php?title=ASP.NET_Core%E3%81%A7JavaScript%E3%81%AEfetch%E9%96%A2%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%86&amp;action=history"/>
	<updated>2026-07-20T21:31:53Z</updated>
	<subtitle>このウィキのこのページに関する変更履歴</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://monobook.org/w/index.php?title=ASP.NET_Core%E3%81%A7JavaScript%E3%81%AEfetch%E9%96%A2%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%86&amp;diff=20279&amp;oldid=prev</id>
		<title>2024年3月7日 (木) 01:39にAdministratorによる</title>
		<link rel="alternate" type="text/html" href="https://monobook.org/w/index.php?title=ASP.NET_Core%E3%81%A7JavaScript%E3%81%AEfetch%E9%96%A2%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%86&amp;diff=20279&amp;oldid=prev"/>
		<updated>2024-03-07T01:39:09Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ja&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← 古い版&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2024年3月7日 (木) 01:39時点における版&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l11&quot;&gt;11行目:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;11行目:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;cshtmlの上部（@pageや@modelなどの直下あたり）に以下を追加する。&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;cshtmlの上部（@pageや@modelなどの直下あたり）に以下を追加する。&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;サーバーサイドでCSRFトークンを生成する。&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;サーバーサイドでCSRFトークンを生成する。&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;source lang=&quot;&quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;source lang=&quot;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;text&lt;/ins&gt;&quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Antiforgery&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Antiforgery&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;@{&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;@{&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://monobook.org/w/index.php?title=ASP.NET_Core%E3%81%A7JavaScript%E3%81%AEfetch%E9%96%A2%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%86&amp;diff=20278&amp;oldid=prev</id>
		<title>2024年3月7日 (木) 01:38にAdministratorによる</title>
		<link rel="alternate" type="text/html" href="https://monobook.org/w/index.php?title=ASP.NET_Core%E3%81%A7JavaScript%E3%81%AEfetch%E9%96%A2%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%86&amp;diff=20278&amp;oldid=prev"/>
		<updated>2024-03-07T01:38:55Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ja&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← 古い版&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2024年3月7日 (木) 01:38時点における版&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l2&quot;&gt;2行目:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;2行目:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== 対策 ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== 対策 ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;　service &lt;/del&gt;===&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;service &lt;/ins&gt;===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;サービスの設定は.NET 8系のテンプレートだとProgram.csにある。&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;サービスの設定は.NET 8系のテンプレートだとProgram.csにある。&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
	<entry>
		<id>https://monobook.org/w/index.php?title=ASP.NET_Core%E3%81%A7JavaScript%E3%81%AEfetch%E9%96%A2%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%86&amp;diff=20277&amp;oldid=prev</id>
		<title>Administrator: ページの作成:「ASP.NET Coreで何も考えずにJavaScriptのfetch関数を使うとCSRF対策機能が働いて400エラー（Bad Request）になります。  == 対策 ==…」</title>
		<link rel="alternate" type="text/html" href="https://monobook.org/w/index.php?title=ASP.NET_Core%E3%81%A7JavaScript%E3%81%AEfetch%E9%96%A2%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%86&amp;diff=20277&amp;oldid=prev"/>
		<updated>2024-03-07T01:38:41Z</updated>

		<summary type="html">&lt;p&gt;ページの作成:「&lt;a href=&quot;/wiki/ASP.NET_Core&quot; title=&quot;ASP.NET Core&quot;&gt;ASP.NET Core&lt;/a&gt;で何も考えずに&lt;a href=&quot;/wiki/JavaScript&quot; title=&quot;JavaScript&quot;&gt;JavaScript&lt;/a&gt;のfetch関数を使うと&lt;a href=&quot;/w/index.php?title=CSRF&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;「CSRF」 (存在しないページ)&quot;&gt;CSRF&lt;/a&gt;対策機能が働いて400エラー（Bad Request）になります。  == 対策 ==…」&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新規ページ&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[ASP.NET Core]]で何も考えずに[[JavaScript]]のfetch関数を使うと[[CSRF]]対策機能が働いて400エラー（Bad Request）になります。&lt;br /&gt;
&lt;br /&gt;
== 対策 ==&lt;br /&gt;
===　service ===&lt;br /&gt;
サービスの設定は.NET 8系のテンプレートだとProgram.csにある。&lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
builder.Services.AddAntiforgery(x =&amp;gt; x.HeaderName = &amp;quot;X-CSRF-TOKEN&amp;quot;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== cshtml ===&lt;br /&gt;
cshtmlの上部（@pageや@modelなどの直下あたり）に以下を追加する。&lt;br /&gt;
サーバーサイドでCSRFトークンを生成する。&lt;br /&gt;
&amp;lt;source lang=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Antiforgery&lt;br /&gt;
@{&lt;br /&gt;
    var requestToken = Antiforgery.GetAndStoreTokens(this.HttpContext).RequestToken;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== javascript ===&lt;br /&gt;
fetch関数のheadersにCSRFトークンを展開する。&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var data = { &amp;quot;head&amp;quot;:&amp;quot;hage&amp;quot; }&lt;br /&gt;
fetch(&amp;#039;/human&amp;#039;, {&lt;br /&gt;
    method: &amp;#039;POST&amp;#039;,&lt;br /&gt;
    headers: {&lt;br /&gt;
        &amp;quot;X-CSRF-TOKEN&amp;quot;: &amp;quot;@requestToken&amp;quot;,&lt;br /&gt;
        &amp;quot;Content-Type&amp;quot;: &amp;quot;application/json&amp;quot;,&lt;br /&gt;
        &amp;quot;Accept&amp;quot;: &amp;quot;application/json&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    body: JSON.stringify(data)&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
おわり&lt;br /&gt;
&lt;br /&gt;
== 関連項目 ==&lt;br /&gt;
* [[ASP.NET CoreのPageModelのOnPost関数の引数でJSONを受け取る]]&lt;br /&gt;
&lt;br /&gt;
[[category: ASP.NET Core]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
</feed>