I'm creating an application that uses FormsAuthenticate to login to the system.
However, I'd like to make a treatment for 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, xhr.status
is returned as 404.
Doing a search, I found that site . The solution given with ApplicationAuthorizeAttribute
: HandleUnauthorizedRequest
is quite what I was imagining.
The main class follows: method
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 do not know how to put this in my code. (my English is not very good). Is there any way for this class to be loaded automatically so overriding can be executed?