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/diagnostics/asp0026.md
+16-5Lines changed: 16 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,11 @@
1
1
---
2
2
title: "ASP0026: Analyzer to warn when [Authorize] is overridden by [AllowAnonymous] from 'farther away'"
3
-
ms.date: 03/27/2025
4
-
description: "Learn about analysis rule ASP0026: [Authorize] is overridden by [AllowAnonymous] from 'farther away'"
3
+
ai-usage: ai-assisted
5
4
author: tdykstra
5
+
description: "Learn about analysis rule ASP0026: [Authorize] is overridden by [AllowAnonymous] from 'farther away'"
6
6
monikerRange: '>= aspnetcore-9.0'
7
7
ms.author: tdykstra
8
+
ms.date: 11/06/2025
8
9
uid: diagnostics/asp0026
9
10
---
10
11
# ASP0026: `[Authorize]` is overridden by `[AllowAnonymous]` from "farther away"
@@ -19,6 +20,9 @@ uid: diagnostics/asp0026
19
20
20
21
It seems intuitive that an `[Authorize]` attribute placed "closer" to an MVC action than an `[AllowAnonymous]` attribute would override the `[AllowAnonymous]` attribute and force authorization. However, this is not necessarily the case. What does matter is the relative order of the attributes.
21
22
23
+
> [!NOTE]
24
+
> The `[AllowAnonymous]` attribute doesn't disable authentication entirely. When credentials are sent to an endpoint with `[AllowAnonymous]`, the endpoint still authenticates those credentials and establishes the user's identity. The `[AllowAnonymous]` attribute only means that authentication is **not required**—the endpoint will run as anonymous only when no credentials are provided. This behavior can be useful for endpoints that need to work for both authenticated and anonymous users.
25
+
22
26
The following code shows examples where a closer `[Authorize]` attribute gets overridden by an `[AllowAnonymous]` attribute that is farther away.
23
27
24
28
```csharp
@@ -58,7 +62,12 @@ public class MyControllerMultiple : ControllerBase
58
62
59
63
## Rule description
60
64
61
-
Warning that an `[Authorize]` attribute is overridden by an `[AllowAnonymous]` attribute from "farther away."
65
+
This warning indicates that an `[Authorize]` attribute is overridden by an `[AllowAnonymous]` attribute from "farther away." When `[AllowAnonymous]` takes precedence, the endpoint doesn't require authentication but still accepts and processes credentials if they're provided. This means:
66
+
67
+
- If a request includes authentication credentials, the endpoint authenticates the user and makes their identity available.
68
+
- If a request doesn't include credentials, the endpoint allows anonymous access.
69
+
70
+
This behavior might unintentionally expose endpoints that were meant to require authentication.
62
71
63
72
## How to fix violations
64
73
@@ -70,8 +79,10 @@ public class MyController
70
79
{
71
80
// This produces no warning because the second, "closer" [AllowAnonymous]
72
81
// clarifies that [Authorize] is intentionally overridden.
73
-
// Specifying AuthenticationSchemes can still be useful
74
-
// for endpoints that allow but don't require authenticated users.
82
+
// Specifying AuthenticationSchemes can be useful for endpoints that
83
+
// allow but don't require authenticated users. When credentials are sent,
84
+
// they will be authenticated; when no credentials are sent, the endpoint
Copy file name to clipboardExpand all lines: aspnetcore/release-notes/aspnetcore-9/includes/asp0026.md
+7-2Lines changed: 7 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,9 @@
2
2
3
3
It seems intuitive that an `[Authorize]` attribute placed "closer" to an MVC action than an `[AllowAnonymous]` attribute would override the `[AllowAnonymous]` attribute and force authorization. However, this is not necessarily the case. What does matter is the relative order of the attributes.
4
4
5
+
> [!NOTE]
6
+
> The `[AllowAnonymous]` attribute doesn't disable authentication entirely. When credentials are sent to an endpoint with `[AllowAnonymous]`, the endpoint still authenticates those credentials and establishes the user's identity. The `[AllowAnonymous]` attribute only means that authentication is **not required**—the endpoint will run as anonymous only when no credentials are provided. This behavior can be useful for endpoints that need to work for both authenticated and anonymous users.
7
+
5
8
The following code shows examples where a closer `[Authorize]` attribute gets overridden by an `[AllowAnonymous]` attribute that is farther away.
6
9
7
10
```csharp
@@ -53,8 +56,10 @@ public class MyController
53
56
{
54
57
// This produces no warning because the second, "closer" [AllowAnonymous]
55
58
// clarifies that [Authorize] is intentionally overridden.
56
-
// Specifying AuthenticationSchemes can still be useful
57
-
// for endpoints that allow but don't require authenticated users.
59
+
// Specifying AuthenticationSchemes can be useful for endpoints that
60
+
// allow but don't require authenticated users. When credentials are sent,
61
+
// they will be authenticated; when no credentials are sent, the endpoint
0 commit comments