ASP.NET-MVC-2 – How to process unauthorizedRequest in AJAX calls in ASP.NET MVC2

Brief:

I have a subclass of AuthorizeAttribute in my frame, and I am doing custom authorization.

I am switching from normal asp.net mvc view rendering to Ajax rendering through jQuery. Therefore, each link in the application performs an ajax call to get data.

To meet this demand, I will Part of the page is converted to a partial view so that each ajax request only gets the part that needs to be updated on the page.

During the normal view rendering, when the request is not authorized, it will be redirected to the web. The login page described in the config. After converting to Ajax, things are a bit different because I don’t want to use the markup of the login page in the ajax request, but want to have a structured response in it so that I can perform actions accordingly in the ajax call .

In order to do this, I believe I have to override the HandleUnauthorizedRequest method in the AuthorizeAttribute class of the subclass and set the filterContext.Result to the json result. But in the process of doing so, how do I distinguish Unauthorized request and successful request, because from the perspective of ajax call, both are successful responses; therefore will be processed in the success handler.

The correct way to deal with this problem is What?

I just figured it out, I can filter between normal requests and ajax requests in HandleUnauthorized
The request method I rewritten in the AuthorizeAttribute subclass. For ajax requests, I can create a json result or other content for this, and for normal requests, it will still display the login page. The code is as follows:

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
{
JsonResult UnauthorizedResult = new JsonResult();
UnauthorizedResult.Data = "{ request:'unauthorized' }";
UnauthorizedResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
filterContext.Result = UnauthorizedResult;
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}

I still won’t transfer my The problem is marked as resolved, so if someone can suggest a better way, I am still willing to accept suggestions.

Brief:

I There is a subclass of AuthorizeAttribute in my frame, and I am doing custom authorization.

I am using jQuery from The normal asp.net mvc view rendering is switched to Ajax rendering. Therefore, each link in the application executes an ajax call to obtain data.

In order to meet this demand, I converted most of the page into partial View so that each ajax request only gets the part that needs to be updated on the page.

During the normal view rendering, when the request is not authorized, it will be redirected to the login described in web.config Page. After converting to Ajax, things are a bit different because I don’t want to use the markup of the login page in the ajax request, but want to have a structured response in it, so that I can perform the action accordingly in the ajax call.

In order to do this, I believe I have to override the HandleUnauthorizedRequest method in the AuthorizeAttribute class of the subclass and set filterContext.Result to the json result. But in the process of doing so, how can I distinguish unauthorized requests And the successful request, because from the perspective of the ajax call, both are successful responses; therefore will be processed in the success handler.

What is the correct way to deal with this problem?

I just figured it out, I can filter between normal requests and ajax requests in HandleUnauthorized
I rewritten in the AuthorizeAttribute subclass Request method. For ajax requests, I can create a json result or other content for this. For normal requests, it will still display the login page. The code is as follows:

 protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
{
JsonResult UnauthorizedResult = new JsonResult();
UnauthorizedResult.Data = "{ request:'unauthorized' }";
UnauthorizedResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
filterContext.Result = UnauthorizedResult;
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}

I still won’t mark my issue as resolved, so if someone can suggest more Good way, I am still willing to accept suggestions.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2118 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.