Fix OAuth state error by configuring proper cookie settings for external authentication #797
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.
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:
During OAuth authentication, ASP.NET Core Identity creates correlation cookies to maintain state:
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
SameSite = SameSiteMode.Lax
to allow cookies on OAuth redirectsSecurePolicy
conditionally (SameAsRequest for dev, Always for production)External Authentication Cookies
IdentityConstants.ExternalScheme
with proper SameSite and Secure policiesExpireTimeSpan
to 15 minutes to accommodate users who take time on the provider's login pageCode Changes
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 implicationsTesting
✅ 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
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.