401 and 403 Behavior in CookieAuthenticationHandler
The CookieAuthenticationHandler class always redirects a user on unauthorized and forbidden. It doesn't seem like there is a way to tell it to use the base implementation of sending back a 401 and 403. Is there a way to get that behavior (without a hack) or is there another auth implementation we should be using?
I assume this is for a WebApi or similar? You should be using Bearer, Windows Auth, Basic, etc.. Cookie auth is for user-interactive flows.
It would be a waste of a good cookie :) to use tokens just for that. This achieves the goal.
services
.Configure<CookieAuthenticationOptions>(o =>
{
o.Notifications = new CookieAuthenticationNotifications()
{
OnApplyRedirect = c =>
{
if (c.Request.IsAjaxCall() && c.Response.StatusCode == 200)
c.Response.StatusCode = 401;
else
c.Response.Redirect(c.RedirectUri);
}
};
});
public static class Helper
{
public static bool IsAjaxCall(this HttpRequest request) => request?.Headers["X-Requested-With"] == "XMLHttpRequest";
}
Thanks, guys. I'm going to go with @MaximBalaganskiy on this one.
Dup of https://github.com/aspnet/Security/issues/508.