You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/security/anti-request-forgery.md
+93-14Lines changed: 93 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
---
2
2
title: Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core
3
+
ai-usage: ai-assisted
3
4
author: tdykstra
4
-
description: Discover how to prevent attacks against web apps where a malicious website can influence the interaction between a client browser and the app.
5
-
ms.author: tdykstra
6
5
content_well_notification: AI-contribution
6
+
description: Discover how to prevent attacks against web apps where a malicious website can influence the interaction between a client browser and the app.
7
7
monikerRange: '>= aspnetcore-3.1'
8
+
ms.author: tdykstra
8
9
ms.custom: mvc
9
10
ms.date: 11/16/2023
10
11
uid: security/anti-request-forgery
11
-
ai-usage: ai-assisted
12
12
---
13
13
# Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core
Set the antiforgery `Cookie` properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
170
+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
172
171
173
172
| Option | Description |
174
173
| --- | --- |
@@ -177,6 +176,22 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
177
176
|<xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.HeaderName%2A>| The name of the header used by the antiforgery system. If `null`, the system considers only form data. |
178
177
|<xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.SuppressXFrameOptionsHeader%2A>| Specifies whether to suppress generation of the `X-Frame-Options` header. By default, the header is generated with a value of "SAMEORIGIN". Defaults to `false`. |
179
178
179
+
180
+
181
+
Some browsers don't allow insecure endpoints to set cookies with a 'secure' flag or overwrite cookies whose 'secure' flag is set (for more information, see [Deprecate modification of 'secure' cookies from non-secure origins](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-alone-01)). Since mixing secure and insecure endpoints is a common scenario in apps, ASP.NET Core relaxes the restriction on the secure policy on some cookies, such as the antiforgery cookie, by setting the cookie's <xref:Microsoft.AspNetCore.Http.CookieBuilder.SecurePolicy%2A> to [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy). Even if a malicious user steals an antiforgery cookie, they also must steal the antiforgery token that's typically sent via a form field (more common) or a separate request header (less common). Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
182
+
183
+
Optionally, you can secure the antiforgery cookie in non-Development environments using Secure Sockets Layer (SSL), over HTTPS only, with the following <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.Cookie%2A?displayProperty=nameWithType> property setting in the app's `Program` file:
184
+
185
+
```csharp
186
+
if (!builder.Environment.IsDevelopment())
187
+
{
188
+
builder.Services.AddAntiforgery(o=>
189
+
{
190
+
o.Cookie.SecurePolicy=CookieSecurePolicy.Always;
191
+
});
192
+
}
193
+
```
194
+
180
195
For more information, see <xref:Microsoft.AspNetCore.Builder.CookieAuthenticationOptions>.
181
196
182
197
## Generate antiforgery tokens with `IAntiforgery`
@@ -481,15 +496,15 @@ With the Synchronizer Token Pattern, only the most recently loaded page contains
481
496
* Only the most recently loaded tab contains a valid antiforgery token.
482
497
* Requests made from previously loaded tabs fail with an error: `Antiforgery token validation failed. The antiforgery cookie token and request token do not match`
483
498
484
-
Consider alternative CSRF protection patterns if this poses an issue.
499
+
Consider alternative CSRF protection patterns if this poses an issue.
485
500
486
501
## Configure antiforgery with `AntiforgeryOptions`
487
502
488
-
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in `Program.cs`:
503
+
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in the app's `Program` file:
Set the antiforgery `Cookie` properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
507
+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
493
508
494
509
| Option | Description |
495
510
| --- | --- |
@@ -498,6 +513,20 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
498
513
|<xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.HeaderName%2A>| The name of the header used by the antiforgery system. If `null`, the system considers only form data. |
499
514
|<xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.SuppressXFrameOptionsHeader%2A>| Specifies whether to suppress generation of the `X-Frame-Options` header. By default, the header is generated with a value of "SAMEORIGIN". Defaults to `false`. |
500
515
516
+
Some browsers don't allow insecure endpoints to set cookies with a 'secure' flag or overwrite cookies whose 'secure' flag is set (for more information, see [Deprecate modification of 'secure' cookies from non-secure origins](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-alone-01)). Since mixing secure and insecure endpoints is a common scenario in apps, ASP.NET Core relaxes the restriction on the secure policy on some cookies, such as the antiforgery cookie, by setting the cookie's <xref:Microsoft.AspNetCore.Http.CookieBuilder.SecurePolicy%2A> to [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy). Even if a malicious user steals an antiforgery cookie, they also must steal the antiforgery token that's typically sent via a form field (more common) or a separate request header (less common). Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
517
+
518
+
Optionally, you can secure the antiforgery cookie in non-Development environments using Secure Sockets Layer (SSL), over HTTPS only, with the following <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.Cookie%2A?displayProperty=nameWithType> property setting in the app's `Program` file:
519
+
520
+
```csharp
521
+
if (!builder.Environment.IsDevelopment())
522
+
{
523
+
builder.Services.AddAntiforgery(o=>
524
+
{
525
+
o.Cookie.SecurePolicy=CookieSecurePolicy.Always;
526
+
});
527
+
}
528
+
```
529
+
501
530
For more information, see <xref:Microsoft.AspNetCore.Builder.CookieAuthenticationOptions>.
502
531
503
532
## Generate antiforgery tokens with `IAntiforgery`
@@ -742,7 +771,7 @@ ASP.NET Core includes three [filters](xref:mvc/controllers/filters) for working
Calling <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllers%2A> does ***not*** enable antiforgery tokens. <xref:Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersWithViews%2A> must be called to have built-in antiforgery token support.
748
777
@@ -753,15 +782,15 @@ With the Synchronizer Token Pattern, only the most recently loaded page contains
753
782
* Only the most recently loaded tab contains a valid antiforgery token.
754
783
* Requests made from previously loaded tabs fail with an error: `Antiforgery token validation failed. The antiforgery cookie token and request token do not match`
755
784
756
-
Consider alternative CSRF protection patterns if this poses an issue.
785
+
Consider alternative CSRF protection patterns if this poses an issue.
757
786
758
787
## Configure antiforgery with `AntiforgeryOptions`
759
788
760
-
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in `Program.cs`:
789
+
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in the apps' `Program` file:
Set the antiforgery `Cookie` properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
793
+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
765
794
766
795
| Option | Description |
767
796
| --- | --- |
@@ -770,6 +799,20 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
770
799
|<xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.HeaderName%2A>| The name of the header used by the antiforgery system. If `null`, the system considers only form data. |
771
800
|<xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.SuppressXFrameOptionsHeader%2A>| Specifies whether to suppress generation of the `X-Frame-Options` header. By default, the header is generated with a value of "SAMEORIGIN". Defaults to `false`. |
772
801
802
+
Some browsers don't allow insecure endpoints to set cookies with a 'secure' flag or overwrite cookies whose 'secure' flag is set (for more information, see [Deprecate modification of 'secure' cookies from non-secure origins](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-alone-01)). Since mixing secure and insecure endpoints is a common scenario in apps, ASP.NET Core relaxes the restriction on the secure policy on some cookies, such as the antiforgery cookie, by setting the cookie's <xref:Microsoft.AspNetCore.Http.CookieBuilder.SecurePolicy%2A> to [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy). Even if a malicious user steals an antiforgery cookie, they also must steal the antiforgery token that's typically sent via a form field (more common) or a separate request header (less common). Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
803
+
804
+
Optionally, you can secure the antiforgery cookie in non-Development environments using Secure Sockets Layer (SSL), over HTTPS only, with the following <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.Cookie%2A?displayProperty=nameWithType> property setting in the app's `Program` file:
805
+
806
+
```csharp
807
+
if (!builder.Environment.IsDevelopment())
808
+
{
809
+
builder.Services.AddAntiforgery(o=>
810
+
{
811
+
o.Cookie.SecurePolicy=CookieSecurePolicy.Always;
812
+
});
813
+
}
814
+
```
815
+
773
816
For more information, see <xref:Microsoft.AspNetCore.Builder.CookieAuthenticationOptions>.
774
817
775
818
## Generate antiforgery tokens with `IAntiforgery`
Set the antiforgery `Cookie` properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
1080
+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
1038
1081
1039
1082
| Option | Description |
1040
1083
| --- | --- |
@@ -1043,6 +1086,42 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
1043
1086
|<xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.HeaderName%2A>| The name of the header used by the antiforgery system. If `null`, the system considers only form data. |
1044
1087
|<xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.SuppressXFrameOptionsHeader%2A>| Specifies whether to suppress generation of the `X-Frame-Options` header. By default, the header is generated with a value of "SAMEORIGIN". Defaults to `false`. |
1045
1088
1089
+
Some browsers don't allow insecure endpoints to set cookies with a 'secure' flag or overwrite cookies whose 'secure' flag is set (for more information, see [Deprecate modification of 'secure' cookies from non-secure origins](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-alone-01)). Since mixing secure and insecure endpoints is a common scenario in apps, ASP.NET Core relaxes the restriction on the secure policy on some cookies, such as the antiforgery cookie, by setting the cookie's <xref:Microsoft.AspNetCore.Http.CookieBuilder.SecurePolicy%2A> to [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy). Even if a malicious user steals an antiforgery cookie, they also must steal the antiforgery token that's typically sent via a form field (more common) or a separate request header (less common). Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
1090
+
1091
+
Optionally, you can secure the antiforgery cookie in non-Development environments using Secure Sockets Layer (SSL), over HTTPS only, with the following <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.Cookie%2A?displayProperty=nameWithType> property setting in the app's `Startup` class:
0 commit comments