Skip to content

Commit f952406

Browse files
authored
Update api-endpoint-auth.md
1 parent ec7ac78 commit f952406

1 file changed

Lines changed: 22 additions & 41 deletions

File tree

aspnetcore/security/authentication/api-endpoint-auth.md

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,42 @@ ms.date: 08/06/2025
99
uid: security/authentication/api-endpoint-auth
1010
---
1111

12+
---
13+
title: API endpoint authentication behavior in ASP.NET Core
14+
author: wadepickett
15+
description todescription: Learn how ASP.NET Core handles authentication failures for API endpoints using cookie authentication.
16+
ai-usage: ai-assisted
17+
monikerRange: '>= aspnetcore-10.0'
18+
ms.author: wpickett
19+
ms.date: 08/06/2025
20+
uid: security/authentication/api-endpoint-auth
21+
---
22+
1223
# API endpoint authentication behavior in ASP.NET Core
1324

1425
:::moniker range=">= aspnetcore-10.0"
1526

16-
When using cookie authentication, API endpoints return the appropriate HTTP status codes (such as 401 or 403) for authentication failures instead of redirecting unauthenticated requests to login pages. This behavior, which is more suitable for programmatic API access, was introduced in ASP.NET Core in .NET 10.
17-
18-
## The problem
27+
When using cookie authentication, API endpoints return appropriate HTTP status codes—such as 401 (Unauthorized) or 403 (Forbidden)—when authentication fails. This approach avoids redirecting unauthenticated requests login pages, which is more suitable for programmatic access scenarios like API clients. This behavior was introduced starting with ASP.NET Core in .NET 10.
1928

20-
In previous versions of ASP.NET Core, when using cookie authentication, all unauthenticated requests would trigger a redirect to the configured login page. This behavior was problematic for API endpoints because:
29+
## How ASP.NET Core identifies API endpoints
2130

22-
- API clients don't expect HTML login pages
23-
- Redirects return 302 status codes instead of proper 401/403 codes
24-
- API clients need clear HTTP status codes to handle authentication failures appropriately
31+
ASP.NET Core automatically applies this behavior to endpoints it recognizes as API-related, including:
2532

26-
## The solution in ASP.NET Core 10
27-
28-
ASP.NET Core 10 automatically detects known API endpoints and modifies the authentication behavior:
29-
30-
- **Web pages**: Continue to redirect to login pages as before
31-
- **API endpoints**: Return appropriate 401 (Unauthorized) or 403 (Forbidden) status codes without redirects
32-
33-
## Which endpoints are considered API endpoints?
34-
35-
The framework automatically identifies the following as API endpoints:
36-
37-
- Controllers decorated with `[ApiController]` attribute
33+
- Controllers decorated with the `[ApiController]` attribute
3834
- Minimal API endpoints registered with `MapGet`, `MapPost`, `MapPut`, `MapDelete`, etc.
3935
- Endpoints that explicitly request JSON responses
4036
- SignalR hubs and endpoints
4137

42-
## Behavior differences
43-
44-
### Before ASP.NET Core 10
38+
## Default behavior and customization
4539

46-
```http
47-
GET /api/secure-data HTTP/1.1
48-
Host: example.com
40+
By default, ASP.NET Core applies cookie authentication logic based on the endpoint type:
4941

50-
HTTP/1.1 302 Found
51-
Location: /Account/Login?ReturnUrl=%2Fapi%2Fsecure-data
52-
```
53-
54-
### ASP.NET Core 10 and later
55-
56-
```http
57-
GET /api/secure-data HTTP/1.1
58-
Host: example.com
59-
60-
HTTP/1.1 401 Unauthorized
61-
WWW-Authenticate: Cookie
62-
```
42+
- **Web pages**: Redirect to login pages
43+
- **API endpoints**: Return 401 or 403 status codes without redirects
6344

6445
## Configuring the behavior
6546

66-
While the default behavior works for most scenarios, you can customize it if needed:
47+
While the default behavior works for most scenarios, it can be customize if needed:
6748

6849
```csharp
6950
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
@@ -75,7 +56,7 @@ builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationSc
7556
});
7657
```
7758

78-
If you need to override the automatic detection for specific endpoints, you can use the `[Authorize]` attribute with specific authentication schemes or implement custom authentication handlers.
59+
If you need to override the automatic detection for specific endpoints, use the `[Authorize]` attribute with specific authentication schemes or implement custom authentication handlers.
7960

8061
## Migration considerations
8162

@@ -106,4 +87,4 @@ public async Task UnauthorizedApiRequest_Returns401()
10687
- <xref:fundamentals/minimal-apis/responses>
10788
- <xref:signalr/authn-and-authz>
10889

109-
:::moniker-range-end
90+
:::moniker-range-end

0 commit comments

Comments
 (0)