[RESOLUTION]
To do what I was intending, I found an implementation where an implementation of an interface IHttpModule
was done.
public class SuppressFormsAuthenticationRedirectModule : IHttpModule {
public void Init(HttpApplication context) {
context.EndRequest += OnEndRequest;
}
private void OnEndRequest(object source, EventArgs args) {
var context = (HttpApplication)source;
var response = context.Response;
var request = context.Request;
if (response.StatusCode == 302 && request.AppRelativeCurrentExecutionFilePath.StartsWith("~"+ConstantesApi.API_URL)) {
response.TrySkipIisCustomErrors = true;
response.ClearContent(); // limpra o conteúdo
response.StatusCode = 401; // Coloca o código de erro http
response.SuppressFormsAuthenticationRedirect = true; // pula o redirecionamento do forms authentication
response.RedirectLocation = null; // tira o redirecionamento
response.Output.Write("{\"message\": \"Erro ao autenticar\"}"); // escreve na saída do response
}
}
public void Dispose() {
}
}
Then I created a file to link the module to the system at runtime
// Carrega o módulo no start da aplicação
[assembly: PreApplicationStartMethod(typeof(FormsAuthenticationConfig), "Register" /* Método chamado na classe*/)]
namespace Unitins.Egresso.MVC.App_Start {
public static class FormsAuthenticationConfig {
public static void Register() {
DynamicModuleUtility.RegisterModule(typeof(SuppressFormsAuthenticationRedirectModule));
}
}
}
Source: link