[RESOLUTION]
To do what I was aiming for, I found an implementation where an implementation of an interface IHttpModule
.
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 on execution
// 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: http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx/
already tried Unauthorized Return(); ?
– Gabriel Heguedusch
@Fábio Lima, could add the problem resolution as an answer, so we will know better that found a solution. rsrs
– Luídne