ASP.NET Core 2.0 disable automatic challenge

前端 未结 8 910
萌比男神i
萌比男神i 2020-11-28 13:40

After upgrading my ASP.NET Core project to 2.0, attempts to access protected endpoints no longer returns 401, but redirects to an (non-existing) endpoint in an attempt to le

8条回答
  •  时光说笑
    2020-11-28 13:55

    This is the source code of CookieAuthenticationEvents.OnRedirectToLogin :

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

    You can add "X-Requested-With: XMLHttpRequest" Header to the request while making API calls from your client.

提交回复
热议问题