Skip to content

Commit 1ba120b

Browse files
Copilotwadepicketttdykstra
authored
Add documentation for .NET 10 API endpoint authentication behavior changes (#35895)
* Initial plan * Add API endpoint authentication behavior documentation for .NET 10 Co-authored-by: wadepickett <[email protected]> * Update placement of API endpoint authentication include based on feedback Co-authored-by: wadepickett <[email protected]> * Update responses.md Moved include for api-endpoint-auth further below the intro * Update cookie.md Moved include to within .NET 10 moniker range. * Update authn-and-authz.md Moved include to moniker range 10 * Update index.md Moved include to version 10 moniker range. * Update aspnetcore/security/authentication/api-endpoint-auth.md * Update api-endpoint-auth.md * Update api-endpoint-auth.md * Update cookie.md * Update authn-and-authz.md fixed moniker range authn-and-authz.md * Update authn-and-authz.md * Update index.md * Update api-endpoint-auth.md * Update authn-and-authz.md * Add API endpoint auth doc to TOC and improve wording Co-authored-by: wadepickett <[email protected]> * Update aspnetcore/security/authentication/api-endpoint-auth.md * Update aspnetcore/security/authentication/api-endpoint-auth.md Co-authored-by: Tom Dykstra <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: wadepickett <[email protected]> Co-authored-by: Wade Pickett <[email protected]> Co-authored-by: Tom Dykstra <[email protected]>
1 parent f5998cb commit 1ba120b

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed

aspnetcore/fundamentals/minimal-apis/responses.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ uid: fundamentals/minimal-apis/responses
1212

1313
[!INCLUDE[](~/includes/not-latest-version.md)]
1414

15+
This article explains how to create responses for minimal API endpoints in ASP.NET Core. Minimal APIs provide several ways to return data and HTTP status codes.
16+
1517
:::moniker range=">= aspnetcore-10.0"
1618

1719
Minimal endpoints support the following types of return values:
@@ -20,6 +22,8 @@ Minimal endpoints support the following types of return values:
2022
1. `T` (Any other type) - This includes `Task<T>` and `ValueTask<T>`.
2123
1. `IResult` based - This includes `Task<IResult>` and `ValueTask<IResult>`.
2224

25+
[!INCLUDE[](~/includes/api-endpoint-auth.md)]
26+
2327
## `string` return values
2428

2529
|Behavior|Content-Type|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
> [!IMPORTANT]
2+
> Starting with ASP.NET Core 10, known API endpoints no longer redirect to login pages when using cookie authentication. Instead, they return 401/403 status codes. For details, see <xref:security/authentication/api-endpoint-auth>.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: API endpoint authentication behavior in ASP.NET Core
3+
author: wadepickett
4+
description: Learn how ASP.NET Core 10 and later handles authentication failures for API endpoints using cookie authentication.
5+
ai-usage: ai-assisted
6+
monikerRange: '>= aspnetcore-10.0'
7+
ms.author: wpickett
8+
ms.date: 08/06/2025
9+
uid: security/authentication/api-endpoint-auth
10+
---
11+
12+
# API endpoint authentication behavior in ASP.NET Core
13+
14+
:::moniker range=">= aspnetcore-10.0"
15+
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+
## How ASP.NET Core identifies API endpoints
19+
20+
ASP.NET Core automatically applies this behavior to endpoints it recognizes as API-related, including:
21+
22+
- Controllers decorated with the `[ApiController]` attribute
23+
- Minimal API endpoints registered with `MapGet`, `MapPost`, `MapPut`, `MapDelete`, etc.
24+
- Endpoints that explicitly request JSON responses
25+
- SignalR hubs and endpoints
26+
27+
## Default behavior and customization
28+
29+
By default, ASP.NET Core applies cookie authentication logic based on the endpoint type:
30+
31+
- **Web pages**: Redirect to login pages
32+
- **API endpoints**: Return 401 or 403 status codes without redirects
33+
34+
## Configuring the behavior
35+
36+
While the default behavior works for most scenarios, it can be customized if needed:
37+
38+
```csharp
39+
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
40+
.AddCookie(options =>
41+
{
42+
options.LoginPath = "/Account/Login";
43+
// The framework automatically handles API endpoints
44+
// No additional configuration needed
45+
});
46+
```
47+
48+
If you need to override the automatic detection for specific endpoints, use the `[Authorize]` attribute with specific authentication schemes or implement custom authentication handlers.
49+
50+
## Migration considerations
51+
52+
This behavior change introduced in .NET 10 is designed to be non-breaking for existing applications:
53+
54+
- **Web applications**: Continue to work as before with login page redirects
55+
- **Mixed applications**: API endpoints get proper status codes while web pages get redirects
56+
- **API-only applications**: Benefit from proper HTTP status codes without additional configuration
57+
58+
### Testing your API endpoints
59+
60+
After upgrading to ASP.NET Core 10, verify that your API endpoints return appropriate status codes:
61+
62+
```csharp
63+
[Test]
64+
public async Task UnauthorizedApiRequest_Returns401()
65+
{
66+
var response = await client.GetAsync("/api/secure-data");
67+
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
68+
Assert.False(response.Headers.Location != null); // No redirect
69+
}
70+
```
71+
72+
## Related topics
73+
74+
- <xref:security/authentication/cookie>
75+
- <xref:web-api/index>
76+
- <xref:fundamentals/minimal-apis/responses>
77+
- <xref:signalr/authn-and-authz>
78+
79+
:::moniker-end

aspnetcore/security/authentication/cookie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ By [Rick Anderson](https://twitter.com/RickAndMSFT)
1919

2020
For demonstration purposes in the sample app, the user account for the hypothetical user, Maria Rodriguez, is hardcoded into the app. Use the **Email** address `[email protected]` and any password to sign in the user. The user is authenticated in the `AuthenticateUser` method in the `Pages/Account/Login.cshtml.cs` file. In a real-world example, the user would be authenticated against a datastore.
2121

22+
:::moniker-end
23+
:::moniker range=">= aspnetcore-10.0"
24+
[!INCLUDE[](~/includes/api-endpoint-auth.md)]
25+
:::moniker-end
26+
:::moniker range=">= aspnetcore-6.0"
2227
## Add cookie authentication
2328

2429
* Add the Authentication Middleware services with the <xref:Microsoft.Extensions.DependencyInjection.AuthenticationServiceCollectionExtensions.AddAuthentication%2A> and <xref:Microsoft.Extensions.DependencyInjection.CookieExtensions.AddCookie%2A> methods.

aspnetcore/signalr/authn-and-authz.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ In a browser-based app, cookie authentication allows existing user credentials t
3030

3131
Cookies are a browser-specific way to send access tokens, but non-browser clients can send them. When using the [.NET Client](xref:signalr/dotnet-client), the `Cookies` property can be configured in the `.WithUrl` call to provide a cookie. However, using cookie authentication from the .NET client requires the app to provide an API to exchange authentication data for a cookie.
3232

33+
:::moniker-end
34+
:::moniker range=">= aspnetcore-10.0"
35+
[!INCLUDE[](~/includes/api-endpoint-auth.md)]
36+
:::moniker-end
37+
:::moniker range=">= aspnetcore-6.0"
38+
3339
### Bearer token authentication
3440

3541
The client can provide an access token instead of using a cookie. The server validates the token and uses it to identify the user. This validation is done only when the connection is established. During the life of the connection, the server doesn't automatically revalidate to check for token revocation.

aspnetcore/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,8 @@ items:
17641764
href: /azure/active-directory-b2c/enable-authentication-web-api
17651765
- name: Configure cookie authentication
17661766
uid: security/authentication/cookie
1767+
- name: API endpoint authentication behavior
1768+
uid: security/authentication/api-endpoint-auth
17671769
- name: Configure OIDC web authentication
17681770
uid: security/authentication/configure-oidc-web-authentication
17691771
- name: Configure JWT bearer authentication

aspnetcore/web-api/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ ASP.NET Core supports creating web APIs using controllers or using minimal APIs.
1616

1717
This article shows how to use controllers for handling web API requests. For information on creating web APIs without controllers, see <xref:tutorials/min-web-api>.
1818

19+
:::moniker-end
20+
:::moniker range=">= aspnetcore-10.0"
21+
[!INCLUDE[](~/includes/api-endpoint-auth.md)]
22+
:::moniker-end
23+
:::moniker range=">= aspnetcore-7.0"
24+
1925
## ControllerBase class
2026

2127
A controller-based web API consists of one or more controller classes that derive from <xref:Microsoft.AspNetCore.Mvc.ControllerBase>. The web API project template provides a starter controller:

0 commit comments

Comments
 (0)