Skip to content

Avoid cookie login redirects for known API endpoints #62816

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

halter73
Copy link
Member

@halter73 halter73 commented Jul 18, 2025

This adds IApiEndpointMetadata as new public API. It doesn't add any attributes or extension methods, but we do add it automatically if RDF/RDG read a JSON request body or write a JSON response body. ApiController endpoints, SignalR endpoints and endpoints with TypedResults return-types that we know to be API-oriented also get this metadata automatically.

I plan to open an API proposal issue shortly to discuss this, and what IResult types (or other types or methods for that matter) should add this metadata.

Fixes #9039

@Copilot Copilot AI review requested due to automatic review settings July 18, 2025 23:57
@halter73 halter73 requested review from BrennanConroy, a team and captainsafia as code owners July 18, 2025 23:57
@github-actions github-actions bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Jul 18, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces IApiEndpointMetadata as a new public interface to identify API endpoints that should avoid cookie authentication redirects in favor of status codes. The interface is automatically added to endpoints that expect JSON request bodies or return JSON responses, signaling they are programmatic APIs rather than browser-navigated endpoints.

Key changes:

  • Adds IApiEndpointMetadata interface and internal implementation
  • Modifies cookie authentication to check for this metadata and return 401/403 status codes instead of redirects
  • Updates HTTP Results types and request delegate generation to automatically add the metadata

Reviewed Changes

Copilot reviewed 107 out of 108 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Http/Http.Abstractions/src/Metadata/IApiEndpointMetadata.cs Defines the new public interface for API endpoint identification
src/Security/Authentication/Cookies/src/CookieAuthenticationEvents.cs Updates authentication logic to check for API endpoints and return status codes
src/Mvc/Mvc.Core/src/ApiControllerAttribute.cs Makes ApiControllerAttribute implement IApiEndpointMetadata
Multiple HTTP Results files Adds IApiEndpointMetadata to various result types' PopulateMetadata methods
Request delegate generator files Updates code generation to automatically add API metadata
Comments suppressed due to low confidence (1)

src/Security/Authentication/test/CookieTests.cs:130

  • The comment references 'INonBrowserEndpointMetadata' but the code adds 'ApiEndpointMetadata.Instance'. The comment should be updated to reflect the actual metadata being added.
        var transaction = await SendAsync(server, "http://example.com/protected/CustomRedirect");

namespace Microsoft.AspNetCore.Http.Metadata;

/// <summary>
/// Metadata that indicates the endpoint is an API intended for programatic access rather than direct browser navigation.
Copy link
Preview

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The word 'programatic' should be spelled 'programmatic'.

Suggested change
/// Metadata that indicates the endpoint is an API intended for programatic access rather than direct browser navigation.
/// Metadata that indicates the endpoint is an API intended for programmatic access rather than direct browser navigation.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ApiController redirects to login page
1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy