Configure Cookie authentication to return 401 for API paths in Server project
This commit is contained in:
@@ -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 =>
|
||||||
|
|||||||
Reference in New Issue
Block a user