UseStatusCodePagesWithReExecute is not working for forbidden (403)

后端 未结 1 811
情书的邮戳
情书的邮戳 2021-01-14 09:14

When I specify 404 as a http result code, UseStatusCodePagesWithReExecute is working like expected.

When I specify 403 as a http result code, <

1条回答
  •  醉梦人生
    2021-01-14 09:55

    Figured it out, thanks to @Kirk

    Adding this code to AddCookie does the trick.

    options.Events.OnRedirectToAccessDenied = context =>
    {
        context.Response.StatusCode = 403;
    
        return Task.CompletedTask;
    };
    

    This is the original event handler method, I don't care the Location header, so I have omitted the related code, you may not want to.

    public Func, Task> OnRedirectToAccessDenied { get; set; } = (Func, Task>) (context =>
    {
        if (CookieAuthenticationEvents.IsAjaxRequest(context.Request))
        {
        context.Response.Headers["Location"] = (StringValues) context.RedirectUri;
        context.Response.StatusCode = 403;
        }
        else
        context.Response.Redirect(context.RedirectUri);
        return Task.CompletedTask;
    });
    

    0 讨论(0)
提交回复
热议问题