-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Add documentation for .NET 10 API endpoint authentication behavior changes #35895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
659fc53
Initial plan
Copilot 941bbdf
Add API endpoint authentication behavior documentation for .NET 10
Copilot f99f5dd
Update placement of API endpoint authentication include based on feed…
Copilot f406310
Update responses.md
wadepickett d7a6d21
Update cookie.md
wadepickett 14ca824
Update authn-and-authz.md
wadepickett b77a554
Update index.md
wadepickett ec7ac78
Update aspnetcore/security/authentication/api-endpoint-auth.md
wadepickett f952406
Update api-endpoint-auth.md
wadepickett fb17d5c
Update api-endpoint-auth.md
wadepickett 70f099f
Update cookie.md
wadepickett 78b9b85
Update authn-and-authz.md
wadepickett ab8fe13
Update authn-and-authz.md
wadepickett c145539
Update index.md
wadepickett 0ba455c
Update api-endpoint-auth.md
wadepickett d490352
Update authn-and-authz.md
wadepickett 636a0e5
Add API endpoint auth doc to TOC and improve wording
Copilot 2373ded
Update aspnetcore/security/authentication/api-endpoint-auth.md
wadepickett 86b8963
Update aspnetcore/security/authentication/api-endpoint-auth.md
wadepickett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
> [!IMPORTANT] | ||
> 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>. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
title: API endpoint authentication behavior in ASP.NET Core | ||
author: wadepickett | ||
description: Learn how ASP.NET Core 10 and later handles authentication failures for API endpoints using cookie authentication. | ||
ai-usage: ai-assisted | ||
monikerRange: '>= aspnetcore-10.0' | ||
ms.author: wpickett | ||
ms.date: 08/06/2025 | ||
uid: security/authentication/api-endpoint-auth | ||
--- | ||
|
||
# API endpoint authentication behavior in ASP.NET Core | ||
|
||
:::moniker range=">= aspnetcore-10.0" | ||
|
||
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. | ||
|
||
## How ASP.NET Core identifies API endpoints | ||
|
||
ASP.NET Core automatically applies this behavior to endpoints it recognizes as API-related, including: | ||
|
||
- Controllers decorated with the `[ApiController]` attribute | ||
- Minimal API endpoints registered with `MapGet`, `MapPost`, `MapPut`, `MapDelete`, etc. | ||
- Endpoints that explicitly request JSON responses | ||
- SignalR hubs and endpoints | ||
|
||
## Default behavior and customization | ||
|
||
By default, ASP.NET Core applies cookie authentication logic based on the endpoint type: | ||
|
||
- **Web pages**: Redirect to login pages | ||
- **API endpoints**: Return 401 or 403 status codes without redirects | ||
|
||
## Configuring the behavior | ||
|
||
While the default behavior works for most scenarios, it can be customize if needed: | ||
|
||
```csharp | ||
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) | ||
.AddCookie(options => | ||
{ | ||
options.LoginPath = "/Account/Login"; | ||
// The framework automatically handles API endpoints | ||
// No additional configuration needed | ||
}); | ||
``` | ||
|
||
If you need to override the automatic detection for specific endpoints, use the `[Authorize]` attribute with specific authentication schemes or implement custom authentication handlers. | ||
|
||
## Migration considerations | ||
|
||
This change is designed to be non-breaking for existing applications: | ||
wadepickett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- **Web applications**: Continue to work as before with login page redirects | ||
- **Mixed applications**: API endpoints get proper status codes while web pages get redirects | ||
- **API-only applications**: Benefit from proper HTTP status codes without additional configuration | ||
|
||
### Testing your API endpoints | ||
|
||
After upgrading to ASP.NET Core 10, verify that your API endpoints return appropriate status codes: | ||
|
||
```csharp | ||
[Test] | ||
public async Task UnauthorizedApiRequest_Returns401() | ||
{ | ||
var response = await client.GetAsync("/api/secure-data"); | ||
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); | ||
Assert.False(response.Headers.Location != null); // No redirect | ||
} | ||
``` | ||
|
||
## Related topics | ||
|
||
- <xref:security/authentication/cookie> | ||
- <xref:web-api/index> | ||
- <xref:fundamentals/minimal-apis/responses> | ||
- <xref:signalr/authn-and-authz> | ||
|
||
:::moniker-end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,11 @@ By [Rick Anderson](https://twitter.com/RickAndMSFT) | |
|
||
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. | ||
|
||
:::moniker-end | ||
:::moniker range=">= aspnetcore-10.0" | ||
[!INCLUDE[](~/includes/api-endpoint-auth.md)] | ||
:::moniker-end | ||
:::moniker range=">= aspnetcore-6.0" | ||
## Add cookie authentication | ||
|
||
* Add the Authentication Middleware services with the <xref:Microsoft.Extensions.DependencyInjection.AuthenticationServiceCollectionExtensions.AddAuthentication%2A> and <xref:Microsoft.Extensions.DependencyInjection.CookieExtensions.AddCookie%2A> methods. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.