Security icon indicating copy to clipboard operation
Security copied to clipboard

401 and 403 Behavior in CookieAuthenticationHandler

Open easyt opened this issue 11 years ago • 4 comments

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?

easyt avatar Sep 29 '15 23:09 easyt

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.

Tratcher avatar Sep 29 '15 23:09 Tratcher

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";
}

MaximBalaganskiy avatar Sep 30 '15 01:09 MaximBalaganskiy

Thanks, guys. I'm going to go with @MaximBalaganskiy on this one.

easyt avatar Sep 30 '15 11:09 easyt

Dup of https://github.com/aspnet/Security/issues/508.

Eilon avatar Oct 08 '15 22:10 Eilon