|
21 | 21 | using Aspire.Dashboard.Otlp.Http; |
22 | 22 | using Aspire.Dashboard.Otlp.Storage; |
23 | 23 | using Aspire.Hosting; |
| 24 | +using Microsoft.AspNetCore.Authentication; |
24 | 25 | using Microsoft.AspNetCore.Authentication.Certificate; |
25 | 26 | using Microsoft.AspNetCore.Authentication.Cookies; |
26 | 27 | using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
@@ -607,7 +608,7 @@ private static bool IsSameOrNull(Uri frontendUri, Uri? otlpUrl) |
607 | 608 | private static void ConfigureAuthentication(WebApplicationBuilder builder, DashboardOptions dashboardOptions) |
608 | 609 | { |
609 | 610 | var authentication = builder.Services |
610 | | - .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) |
| 611 | + .AddAuthentication(o => o.DefaultScheme = ConfigureDefaultAuthScheme(dashboardOptions)) |
611 | 612 | .AddScheme<FrontendCompositeAuthenticationHandlerOptions, FrontendCompositeAuthenticationHandler>(FrontendCompositeAuthenticationDefaults.AuthenticationScheme, o => { }) |
612 | 613 | .AddScheme<OtlpCompositeAuthenticationHandlerOptions, OtlpCompositeAuthenticationHandler>(OtlpCompositeAuthenticationDefaults.AuthenticationScheme, o => { }) |
613 | 614 | .AddScheme<OtlpApiKeyAuthenticationHandlerOptions, OtlpApiKeyAuthenticationHandler>(OtlpApiKeyAuthenticationDefaults.AuthenticationScheme, o => { }) |
@@ -728,6 +729,9 @@ private static void ConfigureAuthentication(WebApplicationBuilder builder, Dashb |
728 | 729 | options.Cookie.Name = DashboardAuthCookieName; |
729 | 730 | }); |
730 | 731 | break; |
| 732 | + case FrontendAuthMode.Unsecured: |
| 733 | + authentication.AddScheme<AuthenticationSchemeOptions, UnsecuredAuthenticationHandler>(FrontendAuthenticationDefaults.AuthenticationSchemeUnsecured, o => { }); |
| 734 | + break; |
731 | 735 | } |
732 | 736 |
|
733 | 737 | builder.Services.AddAuthorization(options => |
@@ -758,13 +762,24 @@ private static void ConfigureAuthentication(WebApplicationBuilder builder, Dashb |
758 | 762 | options.AddPolicy( |
759 | 763 | name: FrontendAuthorizationDefaults.PolicyName, |
760 | 764 | policy: new AuthorizationPolicyBuilder(FrontendCompositeAuthenticationDefaults.AuthenticationScheme) |
761 | | - .RequireClaim(OtlpAuthorization.OtlpClaimName, [bool.FalseString]) |
| 765 | + .RequireClaim(FrontendAuthorizationDefaults.UnsecuredClaimName) |
762 | 766 | .Build()); |
763 | 767 | break; |
764 | 768 | default: |
765 | 769 | throw new NotSupportedException($"Unexpected {nameof(FrontendAuthMode)} enum member: {dashboardOptions.Frontend.AuthMode}"); |
766 | 770 | } |
767 | 771 | }); |
| 772 | + |
| 773 | + // ASP.NET Core authentication needs to have the correct default scheme for the configured frontend auth. |
| 774 | + // This is required for ASP.NET Core/SignalR/Blazor to flow the authenticated user from the request and into the dashboard app. |
| 775 | + static string ConfigureDefaultAuthScheme(DashboardOptions dashboardOptions) |
| 776 | + { |
| 777 | + return dashboardOptions.Frontend.AuthMode switch |
| 778 | + { |
| 779 | + FrontendAuthMode.Unsecured => FrontendAuthenticationDefaults.AuthenticationSchemeUnsecured, |
| 780 | + _ => CookieAuthenticationDefaults.AuthenticationScheme |
| 781 | + }; |
| 782 | + } |
768 | 783 | } |
769 | 784 |
|
770 | 785 | public int Run() |
@@ -804,10 +819,12 @@ public static class FrontendAuthorizationDefaults |
804 | 819 | { |
805 | 820 | public const string PolicyName = "Frontend"; |
806 | 821 | public const string BrowserTokenClaimName = "BrowserTokenClaim"; |
| 822 | + public const string UnsecuredClaimName = "UnsecuredTokenClaim"; |
807 | 823 | } |
808 | 824 |
|
809 | 825 | public static class FrontendAuthenticationDefaults |
810 | 826 | { |
811 | 827 | public const string AuthenticationSchemeOpenIdConnect = "FrontendOpenIdConnect"; |
812 | 828 | public const string AuthenticationSchemeBrowserToken = "FrontendBrowserToken"; |
| 829 | + public const string AuthenticationSchemeUnsecured = "FrontendUnsecured"; |
813 | 830 | } |
0 commit comments