Configure Cookie authentication to return 401 for API paths in Server project

This commit is contained in:
2026-05-08 18:44:33 +00:00
parent 6f1cdfe125
commit 62c8d8a687
+12
View File
@@ -96,6 +96,18 @@ builder.Services.ConfigureApplicationCookie(options =>
options.Cookie.HttpOnly = true; options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromDays(30); options.ExpireTimeSpan = TimeSpan.FromDays(30);
options.SlidingExpiration = true; options.SlidingExpiration = true;
options.Events.OnRedirectToLogin = context =>
{
if (context.Request.Path.StartsWithSegments("/api"))
{
context.Response.StatusCode = 401;
}
else
{
context.Response.Redirect(context.RedirectUri);
}
return Task.CompletedTask;
};
}); });
builder.Services.Configure<IdentityOptions>(options => builder.Services.Configure<IdentityOptions>(options =>