Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 12, 2025

Problem

Users encountered an "oauth state was missing or invalid" error when attempting to log in via external providers (GitHub/Microsoft), which led to a "page not found" page instead of successfully completing the authentication flow.

Root Cause

The application's cookie configuration was missing critical settings required for OAuth authentication flows to work correctly with modern browser security requirements. Specifically:

  1. No SameSite policy configured - Modern browsers require explicit SameSite settings for cookies involved in cross-site redirects
  2. No Secure policy configured - HTTPS environments require explicit secure cookie settings
  3. No external authentication cookie configuration - The OAuth correlation cookies (used to validate state between redirects) had default settings that were insufficient

During OAuth authentication, ASP.NET Core Identity creates correlation cookies to maintain state:

  1. User initiates login → App creates correlation cookie with OAuth state
  2. User redirects to external provider → Correlation cookie must persist
  3. Provider redirects back → App validates state against correlation cookie

Without proper cookie configuration, correlation cookies were either blocked by the browser or not sent during redirects, causing state validation to fail.

Solution

Added proper cookie configuration in Program.cs for both application cookies and external authentication cookies:

Application Cookies

  • Set SameSite = SameSiteMode.Lax to allow cookies on OAuth redirects
  • Set SecurePolicy conditionally (SameAsRequest for dev, Always for production)

External Authentication Cookies

  • Configured IdentityConstants.ExternalScheme with proper SameSite and Secure policies
  • Increased ExpireTimeSpan to 15 minutes to accommodate users who take time on the provider's login page

Code Changes

builder.Services.ConfigureApplicationCookie(options =>
{
    options.Cookie.HttpOnly = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
    options.SlidingExpiration = true;
    // Configure cookie settings for OAuth flows
    options.Cookie.SameSite = SameSiteMode.Lax;
    options.Cookie.SecurePolicy = builder.Environment.IsDevelopment() 
        ? CookieSecurePolicy.SameAsRequest 
        : CookieSecurePolicy.Always;
});

// Configure external authentication cookies for OAuth state management
builder.Services.Configure<CookieAuthenticationOptions>(IdentityConstants.ExternalScheme, options =>
{
    options.Cookie.SameSite = SameSiteMode.Lax;
    options.Cookie.SecurePolicy = builder.Environment.IsDevelopment() 
        ? CookieSecurePolicy.SameAsRequest 
        : CookieSecurePolicy.Always;
    // Increase correlation cookie expiration to handle slow OAuth flows
    options.ExpireTimeSpan = TimeSpan.FromMinutes(15);
});

Why SameSite=Lax?

  • Strict: Would block cookies on OAuth redirects ❌
  • Lax: Allows cookies on top-level navigation (OAuth redirects) ✅
  • None: Would require additional configuration and has security implications

Testing

✅ All existing tests pass (50/51, 1 unrelated network test fails in sandbox environment)
✅ Build succeeds with no errors or warnings
✅ No breaking changes to existing functionality
✅ Changes are minimal and surgical (17 lines added to 1 file)

Impact

This fix ensures OAuth authentication flows work correctly according to modern browser security requirements, resolving login failures for GitHub and Microsoft account authentication.

Fixes #[issue number]

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.hcaptcha.com

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Login error that leads to "page not found" page</issue_title>
<issue_description>An error was encountered while handling the remote login. The oauth state was missing or invalid. |  
</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #796


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Fix login error that leads to page not found Fix OAuth state error by configuring proper cookie settings for external authentication Oct 12, 2025
Copilot finished work on behalf of BenjaminMichaelis October 12, 2025 19:44
@Copilot Copilot AI requested a review from BenjaminMichaelis October 12, 2025 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Login error that leads to "page not found" page

2 participants