I have an ASP.NET Core site that uses cookie authentication for most pages. For those pages, the default server response of providing a 302 redirect for an unauthorized clie
Replace the redirect event handler with one that uses the default behavior only if the path is not an API. In Startup.ConfigureServices
, add this:
services.ConfigureApplicationCookie(options => {
options.Events.OnRedirectToAccessDenied = ReplaceRedirector(HttpStatusCode.Forbidden, options.Events.OnRedirectToAccessDenied);
options.Events.OnRedirectToLogin = ReplaceRedirector(HttpStatusCode.Unauthorized, options.Events.OnRedirectToLogin);
});
Use this helper method to replace the redirect methods:
static Func, Task> ReplaceRedirector(HttpStatusCode statusCode, Func, Task> existingRedirector) =>
context => {
if (context.Request.Path.StartsWithSegments("/api")) {
context.Response.StatusCode = (int)statusCode;
return Task.CompletedTask;
}
return existingRedirector(context);
};
With this in place, the API controller methods can call Unauthorized()
and Forbid()
without causing redirects.
Update: The above is for ASP.NET Core 2. The code for ASP.NET Core 1 is different.