Repository navigation
Fix browser token auth over HTTP #16818
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.AspNetCore.Authentication.Cookies; | ||
|
|
||
| namespace Aspire.Dashboard.Authentication; | ||
|
|
||
| internal sealed class BrowserTokenCookieManager(string httpCookieName) : ICookieManager | ||
| { | ||
| private readonly ChunkingCookieManager _inner = new(); | ||
|
|
||
| public string? GetRequestCookie(HttpContext context, string key) | ||
| { | ||
| return _inner.GetRequestCookie(context, GetCookieName(context, key)); | ||
| } | ||
|
|
||
| public void AppendResponseCookie(HttpContext context, string key, string? value, CookieOptions options) | ||
| { | ||
| _inner.AppendResponseCookie(context, GetCookieName(context, key), value, options); | ||
| } | ||
|
|
||
| public void DeleteCookie(HttpContext context, string key, CookieOptions options) | ||
| { | ||
| _inner.DeleteCookie(context, GetCookieName(context, key), options); | ||
| } | ||
|
|
||
| private string GetCookieName(HttpContext context, string key) | ||
| { | ||
| // Keep HTTP browser-token cookies separate from HTTPS cookies so browser-specific localhost cookie behavior | ||
| // can't cause a stale HTTPS cookie to shadow a fresh HTTP sign-in. | ||
| return context.Request.IsHttps ? key : httpCookieName; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,7 @@ public sealed class DashboardWebApplication : IAsyncDisposable | |
| public const int ExitCodeAddressInUse = DashboardExitCodes.AddressInUse; | ||
|
|
||
| private const string DashboardAuthCookieName = ".Aspire.Dashboard.Auth"; | ||
| private const string DashboardHttpAuthCookieName = ".Aspire.Dashboard.Auth.Http"; | ||
| private const string DashboardAntiForgeryCookieName = ".Aspire.Dashboard.Antiforgery"; | ||
| private readonly WebApplication _app; | ||
| private readonly ILogger<DashboardWebApplication> _logger; | ||
|
|
@@ -464,6 +465,13 @@ public DashboardWebApplication( | |
| _app.UseCors(); | ||
| } | ||
|
|
||
| // Use Forwarded Headers middleware if configured. This must run before token validation because sign-in cookie | ||
| // behavior depends on the normalized request scheme. | ||
| if (builder.Configuration.GetBool(DashboardConfigNames.ForwardedHeaders.ConfigKey) ?? false) | ||
| { | ||
| _app.UseForwardedHeaders(); | ||
| } | ||
|
|
||
| _app.UseMiddleware<ValidateTokenMiddleware>(); | ||
|
|
||
| // Configure the HTTP request pipeline. | ||
|
|
@@ -500,12 +508,6 @@ public DashboardWebApplication( | |
| } | ||
| }); | ||
|
|
||
| // Use Forwarded Headers middleware if configured. | ||
| if (builder.Configuration.GetBool(DashboardConfigNames.ForwardedHeaders.ConfigKey) ?? false) | ||
| { | ||
| _app.UseForwardedHeaders(); | ||
| } | ||
|
|
||
| _app.UseAuthorization(); | ||
|
|
||
| _app.UseMiddleware<BrowserSecurityHeadersMiddleware>(); | ||
|
|
@@ -839,6 +841,7 @@ private static void ConfigureAuthentication(WebApplicationBuilder builder, Dashb | |
| return Task.CompletedTask; | ||
| }; | ||
| options.Cookie.Name = DashboardAuthCookieName; | ||
| options.CookieManager = new BrowserTokenCookieManager(DashboardHttpAuthCookieName); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about OAuth? Should it set this? If so, rename to AspireDashboardCookieManager
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied to OpenID Connect as well in #17368: the manager is now
JamesNK marked this conversation as resolved.
Resolved
|
||
| }); | ||
| break; | ||
| case FrontendAuthMode.Unsecured: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.