Skip to content

Commit c5e2bce

Browse files
committed
fix: use manual auth scheme fallback
1 parent a730190 commit c5e2bce

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

Intersect.Server/Web/Net7/ApiService.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace Intersect.Server.Web;
4343

4444
internal partial class ApiService : ApplicationService<ServerContext, IApiService, ApiService>, IApiService
4545
{
46+
private const string BearerCookieFallbackAuthenticationScheme = "BearerCookieFallback";
4647
private WebApplication? _app;
4748
private static readonly Assembly Assembly = typeof(ApiService).Assembly;
4849

@@ -153,14 +154,7 @@ internal partial class ApiService : ApplicationService<ServerContext, IApiServic
153154

154155
builder.Services.AddSingleton<IntersectAuthenticationManager>();
155156

156-
builder.Services.AddAuthentication(
157-
options =>
158-
{
159-
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
160-
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
161-
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
162-
}
163-
)
157+
builder.Services.AddAuthentication(BearerCookieFallbackAuthenticationScheme)
164158
.AddCookie(
165159
CookieAuthenticationDefaults.AuthenticationScheme,
166160
options =>
@@ -288,6 +282,15 @@ internal partial class ApiService : ApplicationService<ServerContext, IApiServic
288282
SymmetricSecurityKey issuerKey = new(tokenGenerationOptions.SecretData);
289283
options.TokenValidationParameters.IssuerSigningKey = issuerKey;
290284
}
285+
).AddPolicyScheme(
286+
BearerCookieFallbackAuthenticationScheme,
287+
"Bearer-to-Cookie Fallback",
288+
pso =>
289+
{
290+
pso.ForwardDefaultSelector = context => context.Request.Headers.Authorization.Count > 0
291+
? JwtBearerDefaults.AuthenticationScheme
292+
: CookieAuthenticationDefaults.AuthenticationScheme;
293+
}
291294
);
292295

293296
builder.Services.AddOutputCache(o => o.AddPolicy(nameof(AvatarController), AvatarController.OutputCachePolicy));

Intersect.Server/Web/Net7/Middleware/AuthenticationDebugMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public AuthenticationDebugMiddleware(RequestDelegate next, IAuthenticationScheme
2828
public async Task Invoke(HttpContext context)
2929
{
3030
var handlers = context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
31-
var schemes = await Schemes.GetRequestHandlerSchemesAsync();
31+
var schemes = (await Schemes.GetRequestHandlerSchemesAsync()).ToArray();
3232
foreach (var scheme in schemes)
3333
{
3434
if (await handlers.GetHandlerAsync(context, scheme.Name) is IAuthenticationRequestHandler handler &&

0 commit comments

Comments
 (0)