Hi @打玻璃
If you are using Asp.net core identity or cookie authentication, to prevent the Web API redirect to the Login page and show the 401 error, you can override the Cookie Authentication's OnRedirectToLogin event:
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.Events.OnRedirectToAccessDenied =
options.Events.OnRedirectToLogin = c =>
{
c.Response.StatusCode = StatusCodes.Status401Unauthorized;
return Task.FromResult<object>(null);
};
});
After that, when you access the protected action method (the user is not authenticated), it will show the 401 error. The result as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion