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
+91-14Lines changed: 91 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,20 @@ 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
+
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) plus the authentication cookie. Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
180
+
181
+
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:
182
+
183
+
```csharp
184
+
if (!builder.Environment.IsDevelopment())
185
+
{
186
+
builder.Services.AddAntiforgery(o=>
187
+
{
188
+
o.Cookie.SecurePolicy=CookieSecurePolicy.Always;
189
+
});
190
+
}
191
+
```
192
+
180
193
For more information, see <xref:Microsoft.AspNetCore.Builder.CookieAuthenticationOptions>.
181
194
182
195
## Generate antiforgery tokens with `IAntiforgery`
@@ -481,15 +494,15 @@ With the Synchronizer Token Pattern, only the most recently loaded page contains
481
494
* Only the most recently loaded tab contains a valid antiforgery token.
482
495
* 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
496
484
-
Consider alternative CSRF protection patterns if this poses an issue.
497
+
Consider alternative CSRF protection patterns if this poses an issue.
485
498
486
499
## Configure antiforgery with `AntiforgeryOptions`
487
500
488
-
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in `Program.cs`:
501
+
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.
505
+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
493
506
494
507
| Option | Description |
495
508
| --- | --- |
@@ -498,6 +511,20 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
498
511
|<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
512
|<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
513
514
+
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) plus the authentication cookie. Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
515
+
516
+
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:
517
+
518
+
```csharp
519
+
if (!builder.Environment.IsDevelopment())
520
+
{
521
+
builder.Services.AddAntiforgery(o=>
522
+
{
523
+
o.Cookie.SecurePolicy=CookieSecurePolicy.Always;
524
+
});
525
+
}
526
+
```
527
+
501
528
For more information, see <xref:Microsoft.AspNetCore.Builder.CookieAuthenticationOptions>.
502
529
503
530
## Generate antiforgery tokens with `IAntiforgery`
@@ -742,7 +769,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
775
@@ -753,15 +780,15 @@ With the Synchronizer Token Pattern, only the most recently loaded page contains
753
780
* Only the most recently loaded tab contains a valid antiforgery token.
754
781
* 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
782
756
-
Consider alternative CSRF protection patterns if this poses an issue.
783
+
Consider alternative CSRF protection patterns if this poses an issue.
757
784
758
785
## Configure antiforgery with `AntiforgeryOptions`
759
786
760
-
Customize <xref:Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions> in `Program.cs`:
787
+
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.
791
+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
765
792
766
793
| Option | Description |
767
794
| --- | --- |
@@ -770,6 +797,20 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
770
797
|<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
798
|<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
799
800
+
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) plus the authentication cookie. Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
801
+
802
+
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:
803
+
804
+
```csharp
805
+
if (!builder.Environment.IsDevelopment())
806
+
{
807
+
builder.Services.AddAntiforgery(o=>
808
+
{
809
+
o.Cookie.SecurePolicy=CookieSecurePolicy.Always;
810
+
});
811
+
}
812
+
```
813
+
773
814
For more information, see <xref:Microsoft.AspNetCore.Builder.CookieAuthenticationOptions>.
774
815
775
816
## 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.
1078
+
Set the antiforgery cookie properties using the properties of the <xref:Microsoft.AspNetCore.Http.CookieBuilder> class, as shown in the following table.
1038
1079
1039
1080
| Option | Description |
1040
1081
| --- | --- |
@@ -1043,6 +1084,42 @@ Set the antiforgery `Cookie` properties using the properties of the <xref:Micros
1043
1084
|<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
1085
|<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
1086
1087
+
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) plus the authentication cookie. Cookies related to authentication or authorization use a stronger policy than [`CookieSecurePolicy.None`](xref:Microsoft.AspNetCore.Http.CookieSecurePolicy).
1088
+
1089
+
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