0
I am creating an application that uses Formsauthenticate to log into the system.
However, I would like to make a treatment so that in an ajax request (jquery) made when the user is not logged in.
I put a $(document).ajaxComplete(function (event, xhr, settings) {}))
. However, when the server informs that there is no login and gives a redirect to the login page, the xhr.status
is returned as 404.
Doing some research, I found this site. The solution given with ApplicationAuthorizeAttribute
:HandleUnauthorizedRequest
is just what I was imagining.
The main class is as follows:
public class ApplicationAuthorizeAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
var httpContext = filterContext.HttpContext;
var request = httpContext.Request;
var response = httpContext.Response;
var user = httpContext.User;
if (request.IsAjaxRequest())
{
if (user.Identity.IsAuthenticated == false)
response.StatusCode = (int)HttpStatusCode.Unauthorized;
else
response.StatusCode = (int)HttpStatusCode.Forbidden;
response.SuppressFormsAuthenticationRedirect = true;
response.End();
}
base.HandleUnauthorizedRequest(filterContext);
}
}
I just don’t know how to put this in my code. (my English is not very good). Has some way this class is automatically loaded so that Overriding can be executed?